高级查询

高级查询

依赖处理

依赖性(或序列性)查询在执行前要依赖先前的查询完成。要做到这一点,就像使用启用选项来告诉查询何时可以运行一样简单。

// Get the user
const { data: user } = useQuery(["user", email], getUserByEmail);

const userId = user?.id;

// Then get the user's projects
const { isIdle, data: projects } = useQuery(
  ["projects", userId],
  getProjectsByUser,
  {
    // The query will not execute until the userId exists
    enabled: !!userId,
  }
);

// isIdle will be `true` until `enabled` is true and the query begins to fetch.
// It will then go to the `isLoading` stage and hopefully the `isSuccess` stage :)
下一页