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

fix: ensure hook subscription failures do not reset isLoading state #4364

Merged
merged 3 commits into from May 9, 2024

Conversation

smacpherson64
Copy link
Contributor

This PR aims to resolve an issue where the isLoading state of a parent component was being reset by a child component if the child component was rendered conditionally.

Relates to: #1349 (reply in thread)

Copy link

codesandbox bot commented Apr 22, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

codesandbox-ci bot commented Apr 22, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit e5a9962:

Sandbox Source
rsk-github-issues-example Configuration
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration
rtk-esm-cra Configuration

Copy link

netlify bot commented Apr 22, 2024

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit e5a9962
🔍 Latest deploy log https://app.netlify.com/sites/redux-starter-kit-docs/deploys/6626df30ebe6450008100a63
😎 Deploy Preview https://deploy-preview-4364--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@phryneas
Copy link
Member

That won't prevent a refetch though, just display isFetching instead of isLoading. Is that really your problem here?

@smacpherson64
Copy link
Contributor Author

smacpherson64 commented Apr 22, 2024

@phryneas true! This will still fetch twice. I tried to scope this change to only avoid the infinite loop where isLoading reverts back to true for the parent when rendering descendants conditionally.

The refetchOnError idea from the discussion would be much nicer as it would only fetch once.

My specific problem is working with endpoints that can fail (often 401 or 404). The app has a rather deep tree and multiple parts of it need the same data. I assumed I could rely on the isLoading to only be true for the first request (including errors) but this assumption proved false. This caused the app to hit an infinite loop. I was able to avoid it by wrapping the hook with useQueryState to check for a previous error (to mimic the discussion), but it is not the ideal end state.

  return (args, {refetchOnError = true, ...options}) => {
    const {isError} = endpoint.useQueryState(args, options)

    return useHook(args, {
      ...options,
      skip: !!options.skip || (isError && !refetchOnError),
    })
  }

EDIT: adds more specific response and clarity to what is being aimed for.

@markerikson markerikson merged commit 5d77624 into reduxjs:master May 9, 2024
27 checks passed
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

Successfully merging this pull request may close these issues.

None yet

3 participants