Skip to content
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

initialValue is always re-used for subsequent fetches if provided #249

Open
shrugs opened this issue Jan 23, 2020 · 0 comments
Open

initialValue is always re-used for subsequent fetches if provided #249

shrugs opened this issue Jan 23, 2020 · 0 comments

Comments

@shrugs
Copy link

shrugs commented Jan 23, 2020

Context

providing initialValue to useAsync blocks subsequent requests. this is unexpected behavior when server side rendering, where initialValue is provided to hydrate the client, but subsequent requests should fetch new information

Expectation

when start() is called due to watch, watchFn, or reload(), initialValue is ignored and the fetch is re-run with the new arguments

Actual

because no requests have actually be fired, counter.current is always 0 and there are no last args, meaning load is always called for subsequent refetches and early exits due to https://github.com/async-library/react-async/blob/next/packages/react-async/src/useAsync.tsx#L156

Workaround

the only two places where counter.current++ is called are in cancel() and start() (where it is never reachable). calling cancel() (while a request is not inflight) increments counter and allows subsequent requests to proceed as expected

const { data, error, isPending, cancel } = useAsync<any>({
    promiseFn,
    initialValue,
    myArg,
    watch: myArg,
  });

  useEffect(() => {
    cancel();
  }, [cancel]);
@shrugs shrugs changed the title initialValue is always re-used for subsequent fetches, regardless of initialValue is always re-used for subsequent fetches if provided Jan 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant