-
Notifications
You must be signed in to change notification settings - Fork 94
Allow passing arguments to run
with useFetch
#75
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
Comments
Hi Lenz, thanks for the feedback. That's an excellent suggestion. I would love to accept a PR for this. What you're looking for is right here. Be aware that |
I think we should have something like this: (args, props, ctrl) => doFetch(input, ctrl ? { signal: ctrl.signal, ...init, ...args[0] } : { signal: props.signal, ...init }) Note that this spreads only the first argument of |
run
of useAsyncFetchrun
with useFetch
This is merged and will be released in v8.0.0 soon. |
Released in v8.0.0 |
Is there a way to pass dynamic query params to a |
@iamandyk Good question! I thought this was possible, but upon further investigation, it turns out you can't, at least not using For the time being you can fall back to using import { useAsync } from "react-async"
const loadCustomer = async ([id], props, { signal }) => {
const res = await fetch(`/api/customers?id=${id}`, { signal })
if (!res.ok) throw new Error(res)
return res.json()
}
const MyComponent = () => {
const { data, run } = useAsync({ deferFn: loadCustomer })
return (
<div>
{[1, 2, 3].map(id => <Button onClick={() => run(id)}>load #{id}</Button>}
</div>
)
} We'll have to update |
I opened #111 to resolve this. |
Hi there,
I have the following use case:
I want to use
useAsyncFetch
in a function component withformik
.That looks something like this:
But currently, that doesn't work, because I have no way of passing arguments into
run
that actually reflect in thefetch
call - and since myvalues
are only available within theonSubmit
callback of formik, I have no way of accessing those in the outer scope whereuseFetch
is called.(Of course, this can be worked around by keeping
init
in a ref and modifying it before callingrun
, but that's really smelly)So my suggestion would be to use the first argument of this function that is currently unused to allow passing an argument that will be spread over
init
, before the request is sent.Would something like this be feasible? I'd be happy to do a PR if this is something you'd accept.
The text was updated successfully, but these errors were encountered: