我有这个query.me
从 graphql 获取数据的自定义钩子。该console.log
语句显示此挂钩在页面加载时运行了多次,但其中只有 1 次console.logs()
包含实际数据。
import { useCustomQuery } from '../api-client';
export const useMe = () => {
const { data, isLoading, error } = useCustomQuery({
query: async (query) => {
return getFields(query.me, 'account_id', 'role', 'profile_id');
},
});
console.log(data ? data.account_id : 'empty');
return { isLoading, error, me: data };
};
然后我有另一个钩子,它应该使用上面钩子中的 id 从服务器获取更多数据。
export const useActivityList = () => {
const { me, error } = useMe();
const criteria = { assignment: { uuid: { _eq: me.profile_id } } } as appointment_bool_exp;
const query = useQuery({
prepare({ prepass, query }) {
prepass(
query.appointment({ where: criteria }),
'scheduled_at',
'first_name',
'last_name',
);
},
suspense: true,
});
const activityList = query.appointment({ where: criteria });
return {
activityList,
isLoading: query.$state.isLoading,
};
};
我面临的问题是第二个钩子似乎在me
仍未定义时调用了第一个钩子,因此出错了。如何配置它,以便仅在me
填充值时访问?
我不擅长异步的东西......