关于FavouriteGameButton的问题:图标未反映变更结果

huangapple go评论70阅读模式
英文:

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&lt;Props&gt; = ({ gameId }) =&gt; {
  const { user } = useUser();
  if (!user) {
    return null;
  }
  const [{ data: userData }] = useUserQuery({
    variables: { userId: user.id },
  });
  const favouriteGame = userData?.user?.favouriteGame.map((it) =&gt; it.game.id);
  return (
    &lt;FavouriteGameButton
      gameId={gameId}
      isFavourite={favouriteGame?.includes(gameId) || false}
    /&gt;
  );
};

👇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&lt;Props&gt; = ({ gameId, isFavourite }) =&gt; {
  const { user } = useUser();
  const [, updateFavouriteGame] = useUpdateFavouriteGameMutation();
  if (!user) {
    return null;
  }
  const handleUpdateFavouriteGame = async () =&gt; {
    await updateFavouriteGame({
      userId: user.id,
      gameId: gameId,
      action: isFavourite ? &quot;remove&quot; : &quot;add&quot;,
    });
  };
  return (
    &lt;ButtonComponent
      size={&quot;normal&quot;}
      hovercolor=&quot;transparent&quot;
      backgroundcolor=&quot;transparent&quot;
      padding=&quot;0px&quot;
      onClick={() =&gt; {
        handleUpdateFavouriteGame();
      }}
    &gt;
      {isFavourite ? (
        &lt;Icon icon={&quot;starFill&quot;} size=&quot;M&quot; color={Colors.TagText} /&gt;
      ) : (
        &lt;Icon icon={&quot;starOutline&quot;} size=&quot;M&quot; color={Colors.TagText} /&gt;
      )}
    &lt;/ButtonComponent&gt;
  );
};

答案1

得分: 0

  1. 你可以在updateFavouriteGame变异完成后重新获取你的useUserQuery查询。重新获取是变异操作的一种选项。
  2. 你可以将isFavourite视为你的GamePageTop组件的状态,在组件级别传递该状态的setState函数给FavouriteGameButton组件,然后在查询完成后更新该状态。
英文:

You have a couple choices.

  1. You can refetch your useUserQuery query when your updateFavouriteGame mutation completes. Refetching is an option of a mutation operation.
  2. You can treat isFavourite as state at the level of your GamePageTop component, pass the setState function for that state down to the FavouriteGameButton component, and then update that state when your query completes.

huangapple
  • 本文由 发表于 2023年7月27日 23:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定