Skip to content

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

Open

Description

@shrugs

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]);

Activity

  1. changed the title [-]initialValue is always re-used for subsequent fetches, regardless of [/-] [+]initialValue is always re-used for subsequent fetches if provided[/+] on Jan 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions