-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation on how to best chain requests. #233
Comments
Apparentely you can await the get function returned by axios-hooks: export default (valueZero: string): ResponseValues<string> => {
const [{
data: valueOne,
error: errorOne,
loading: loadingOne,
}, getValueOne] = useAxios(`/api/${valueZero}`, {manual: true});
const [{
data: valueTwo = '',
error: errorTwo,
loading: loadingTwo,
}, getValueTwo] = useAxios('', {manual: true});
useEffect(() => {
let isCancelled = false;
const getValueOneThanTwo = async () => {
const response = await getValueOne(undefined, {useCache: true});
const valueOne = response.data.valueOne;
if (valueOne && !isCancelled) {
getValueTwo(`api/${valueOne}`, {useCache: true});
}
}
if (valueOne > 0) {
getValueOneThanTwo()
}
return () => isCancelled = true;
}, [valueZero, getValueOne, getValueTwo]);
// wrapping it as a ResponseType for convenience ;)
return {
data: valueTwo,
error: errorOne || errorTwo,
loading: loadingOne || loadingTwo,
};
}; it still looks a bit complicated. But it has improved the code. A perfect solution would be to somehow tell useAxios when to run the query in the qeury parameters. something like: export default (valueZero: string): ResponseValues<string> => {
const [{
data: valueOne,
error: errorOne,
loading: loadingOne,
}, getValueOne] = useAxios(`/api/${valueZero}`, {runWhen: () => valueZero.length});
// this wont work, dont try to copy paste it
const [{
data: valueTwo = '',
error: errorTwo,
loading: loadingTwo,
}, getValueTwo] = useAxios(`/api/${valueOne}`, {runWhen: () => valueOne.length});
// wrapping it as a ResponseType for convenience ;)
return {
data: valueTwo,
error: errorOne || errorTwo,
loading: loadingOne || loadingTwo,
};
}; But I'd need to dig into to the code to see if something like this is even possible, and know the creators opinion if it is something desirable at all |
This question was asked in this issue: #166. Unfortunately the link in my comment is gone so you can't see the code, but it's conceptually similar to what you did above. Chaining multiple requests consists in awaiting the promise returned by the I'll take note to:
As for the |
Hello!
I have been looking everywhere and trying to have a go at this myself, but I'm still haven't found a pattern that allows me to chain two axios hooks sequentially. Using vanilla axios I could do something like this:
however this kind of solution inside of a useEffect is kind of meh, since I loose all the goodies from axios-hooks. These are the functionalities from axios-hooks i want to keep leveraging:
The problems i'm facing are:
await getValueOne
and callgetValueTwo
immediatly afterHere is my current implementation of a 2 sequential axios hooks calls:
This is incredibly ugly and I was hoping we could brainstorm a bit here to come up with a better alternative to my approach. My motivations to want to keep axios-hooks and not use axios only are:
Any ideas how can we make this better?
The text was updated successfully, but these errors were encountered: