我试图每时让用户以“类似”的语气来回答所有询问,但迄今为止,尽管有该书,还是没有成功。
我有一个获得数据的构成部分:
const {
data: resultsEnCours,
isLoading,
isError,
} = useQueryGetEvents("homeencours", { currentEvents: true });
这是一种习惯做法,可视此:
const useQueryGetEvents = (nameQuery, params, callback) => {
const [refetch, setRefetch] = React.useState(null);
const refetchData = () => setRefetch(Date.now()); // => manual refresh
const { user } = React.useContext(AuthContext);
const { location } = React.useContext(SearchContext);
const { isLoading, isError, data } = useQuery(
[nameQuery, refetch],
() => getFromApi(user.token, "getEvents", { id_user: user.infoUser.id, ...params }),
// home params => "started", "upcoming", "participants"
{
select: React.useCallback(({ data }) => filterAndSortByResults(data, location, callback), []),
}
);
return { data, isLoading, isError, refetchData };
};
export default useQueryGetEvents;
我还有一个组成部分“ButtonLikeEvent”,使用户能够喜欢或不喜欢一件事:
import { useMutation, useQueryClient } from "react-query";
import { postFromApi } from "../../api/routes";
...other imports
const ButtonLikeEvent = ({ item, color = "#fb3958" }) => {
const queryClient = useQueryClient();
const {
user: {
token,
infoUser: { id },
},
} = React.useContext(AuthContext);
const [isFavorite, setIsFavorite] = React.useState(item.isFavorite);
const likeEventMutation = useMutation((object) => postFromApi(token, "postLikeEvent", object));
const dislikeEventMutation = useMutation((object) =>
postFromApi(token, "postDislikeEvent", object)
);
const callApi = () => {
if (!isFavorite) {
likeEventMutation.mutate(
{ id_events: item.id, id_user: id },
{
onSuccess() {
queryClient.invalidateQueries();
console.log("liked");
},
}
);
} else {
dislikeEventMutation.mutate(
{ id_events: item.id, id_user: id },
{
onSuccess() {
queryClient.invalidateQueries();
console.log("disliked");
},
}
);
}
};
return (
<Ionicons
onPress={() => {
setIsFavorite((prev) => !prev);
callApi();
}}
name={isFavorite ? "heart" : "heart-outline"}
size={30}
color={color} //
/>
);
};
export default ButtonLikeEvent;
Every time an user click on that button I d like to invalidate queries (because many screens shows the like button).
The console. 记录显示是成功的,但查询并不无效。
任何想法?
感谢