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
useEffect is never invoked in suspended component #18749
Comments
This is expected. |
With fetch-on-render, you have to have the read itself kick off the fetch. (Which is why fetch-on-render isn’t great, generally speaking — for example, it leads to waterfalls with nested components.) Indeed, if a component's rendering suspends, any effects of that render are discarded. Effects only run for successful renders that haven't been suspended. |
Thank you very much for your quick answers @vkramskikh and @gaearon and for the technical explanation. I'll have to move my fetching logic to the top level component or figure out a way to access React Router props and context to render-as-I-fetch. |
There are no good ergonomic solutions for render-as-you-fetch without either manually hoisting data fetching logic upwards (annoying) or letting a compiler do it (like Relay does). We're working on something in that vein but it's very early so we don't have anything to share yet. |
Hi, sorry to comment on a closed ticket, but I'm not sure where to ask this. @gaearon have you guys considered making a |
I'm trying to Fetch-On-Render (inside a
useEffect
hook) some data that depends on my component's props.My component tree is wrapped in a
Suspense
component and the component that fetches data is a couple of levels down the tree.Because the
useEffect
hook is in a suspended component it never gets invoked.I'm not sure is this is the expected behaviour or a bug.
React version:
Experimental:
Steps To Reproduce
Link to code example:
https://codesandbox.io/s/practical-burnell-75rqm?file=/src/index.js
The current behavior
The
useEffect
hook inProfileDetails
is never invoked and the app stays in a loading state forever.The expected behavior
I'd expect the hook to be called and the data to be loaded.
At the moment the only solution is to move all the data fetching logic outside the
Suspense
component, in my example toProfilePage
The text was updated successfully, but these errors were encountered: