英文:
Trouble with FavouriteGameButton: Icon not reflecting mutation result
问题
我面临一个与"收藏游戏"按钮组件有关的问题。当用户尝试收藏或取消收藏游戏时,图标不会立即根据变更结果进行更新。有人可以提供一些见解或帮助我找到解决这个问题的方法吗?谢谢!
🌟 GamePageTop.tsx 会导入 FavouriteGameButton:接收一个 gameId 属性并检查是否有已登录的用户。如果有,它会获取用户数据并提取收藏的游戏。
type Props = {
gameId: string;
};
export const GamePageTop: FC<Props> = ({ gameId }) => {
const { user } = useUser();
if (!user) {
return null;
}
const [{ data: userData }] = useUserQuery({
variables: { userId: user.id },
});
const favouriteGame = userData?.user?.favouriteGame.map((it) => it.game.id);
return (
<FavouriteGameButton
gameId={gameId}
isFavourite={favouriteGame?.includes(gameId) || false}
/>
);
};
🌟 FavouriteGameButton.tsx 接收 gameId 和 isFavourite 属性。在组件内部,它使用 updateFavouriteGameMutation 处理用户的收藏/取消收藏操作。
type Props = {
gameId: string;
isFavourite: boolean;
};
export const FavouriteGameButton: FC<Props> = ({ gameId, isFavourite }) => {
const { user } = useUser();
const [, updateFavouriteGame] = useUpdateFavouriteGameMutation();
if (!user) {
return null;
}
const handleUpdateFavouriteGame = async () => {
await updateFavouriteGame({
userId: user.id,
gameId: gameId,
action: isFavourite ? "remove" : "add",
});
};
return (
<ButtonComponent
size="normal"
hovercolor="transparent"
backgroundcolor="transparent"
padding="0px"
onClick={() => {
handleUpdateFavouriteGame();
}}
>
{isFavourite ? (
<Icon icon="starFill" size="M" color={Colors.TagText} />
) : (
<Icon icon="starOutline" size="M" color={Colors.TagText} />
)}
</ButtonComponent>
);
};
希望这能帮助你解决问题!
英文:
I'm facing an issue with the Favourite Game button component. When a user tries to favorite or unfavorite a game, the icon doesn't update immediately based on the mutation result. Can anyone offer some insights or help me find a solution to this problem? Thank you!
👇 GamePageTop.tsx that will import FavouriteGameButton: receives a gameId prop and checks if there's a logged-in user. If there is, it fetches the user data and extracts the favorite games.
type Props = {
gameId: string;
};
export const GamePageTop: FC<Props> = ({ gameId }) => {
const { user } = useUser();
if (!user) {
return null;
}
const [{ data: userData }] = useUserQuery({
variables: { userId: user.id },
});
const favouriteGame = userData?.user?.favouriteGame.map((it) => it.game.id);
return (
<FavouriteGameButton
gameId={gameId}
isFavourite={favouriteGame?.includes(gameId) || false}
/>
);
};
👇FavouriteGameButton.tsx that receives the gameId and isFavourite prop. Inside the component, it uses the updateFavouriteGameMutation to handle the user's favorite/unfavorite action.
type Props = {
gameId: string;
isFavourite: boolean;
};
export const FavouriteGameButton: FC<Props> = ({ gameId, isFavourite }) => {
const { user } = useUser();
const [, updateFavouriteGame] = useUpdateFavouriteGameMutation();
if (!user) {
return null;
}
const handleUpdateFavouriteGame = async () => {
await updateFavouriteGame({
userId: user.id,
gameId: gameId,
action: isFavourite ? "remove" : "add",
});
};
return (
<ButtonComponent
size={"normal"}
hovercolor="transparent"
backgroundcolor="transparent"
padding="0px"
onClick={() => {
handleUpdateFavouriteGame();
}}
>
{isFavourite ? (
<Icon icon={"starFill"} size="M" color={Colors.TagText} />
) : (
<Icon icon={"starOutline"} size="M" color={Colors.TagText} />
)}
</ButtonComponent>
);
};
答案1
得分: 0
- 你可以在
updateFavouriteGame变异完成后重新获取你的useUserQuery查询。重新获取是变异操作的一种选项。 - 你可以将
isFavourite视为你的GamePageTop组件的状态,在组件级别传递该状态的setState函数给FavouriteGameButton组件,然后在查询完成后更新该状态。
英文:
You have a couple choices.
- You can refetch your
useUserQueryquery when yourupdateFavouriteGamemutation completes. Refetching is an option of a mutation operation. - You can treat
isFavouriteas state at the level of yourGamePageTopcomponent, pass thesetStatefunction for that state down to theFavouriteGameButtoncomponent, and then update that state when your query completes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论