我有一个函数来触发 apiCall,它需要来自另一个 useQuery 调用 useXXXXX
的一些飞行数据。因此,当我触发 onClickHandler 数据更改以使 App 呈现但来自 useXXXXX
的响应可能会延迟并具有旧响应
function useAPICall(data) => {
const {secondApiCall} = useXXXXX(data) // another useQuery api Call
const query = useQuery(
[{query: apiGQL, variables: {...data}}],
async () => {
const response: {data} = await ucFetch(apiUrl, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
query: apiUrl,
variables: {...data, keyValue: secondApiCall},
}),
});
return response;
},
);
return query;
}
export default function App() {
const {data} = useSelector((state) => state.data)
const {response} = useAPICall(data)
return (
<div className="App">
<button onClick={onClickHandler}>run query</button>
</div>
);
}