Async actions
Last updated
Was this helpful?
Last updated
Was this helpful?
Fetching data for display alone isn't sufficient for most applications. You'll often also want to submit data back to the server, or handle other types of asynchronous actions. To enable this, React Async has the concept of a .
Like promiseFn
, a deferFn
is a function that returns a Promise. The difference is that deferFn
will not be automatically invoked by React Async when rendering the component. Instead it will have to be triggered by calling the function provided by React Async.
useFetch
As you can see, the deferFn
is invoked with 3 arguments: args
, props
and the AbortController. args
is an array representing the arguments that were passed to run
. In this case we passed the email
, so we can extract that from the args
array at the first index using and pass it along to our fetch
request.
The above example can be simplified when we rely on instead of constructing the request manually.
The function for useFetch
is a little special because it allows you to override the request's resource and other params. This way you can pass in the body, add dynamic headers or override the URL.