-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
<Switch> does not update inside <Suspense> #7137
Comments
Actually, this is the expected behaviour of |
Thanks for swift reply. Sorry to contradict you, but I'm not clear that this is intended behavior. Reimplementing https://codesandbox.io/s/react-router-yp42p I wouldn't have expected that change should make any difference, if this was the intended behavior. But have you seen something to the contrary in React's docs somewhere? Am I missing the point? Either way, having to wrap every route in its own Suspense rules out some nice patterns e.g.: <Suspense fallback="Loading">
<div>Page header</div>
<Switch>
<Route ... />
</Switch>
</Suspense> If you want to keep "Page header" hidden until the content of the route has loaded, that is not possible by wrapping each route in its own Suspense. You'd have to add "Page header" to every route. |
The point is, that as long as there is any child component of a <Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/about">
<Suspense fallback={<p>Loading ...</p>}>
<About />
</Suspense>
</Route>
</Switch> EDIT: I looked at your code sandbox example and I think I get your point now. I am not sure if we have to change anything in the implementation but your expected behaviour seem reasonable to me. |
OK, I see your point. However, can you offer any pointers as to why it doesn't work like that with a https://codesandbox.io/s/react-router-yp42p By the way, I'm not trying to pick a fight. The exact semantics of Suspense I don't think are well explained in React's docs, so I'm interested in other peoples' interpretations. I think it's an open question as to whether the Suspense content is "in" the |
Oh, I hadn't seen your edit when I wrote last comment. So do you think this is a bug in React then that |
@overlookmotel No, I don't think that it is a bug of React, but a not implemented feature of RR. |
Hi @timdorr. The example you're looking at is the one where I've re-implemented react-router's Please see the original test case for what happens with unaltered React Router: |
Ah, that's why. Duh. Might be related to facebook/react#17356 |
You do not have to wrap every component with Suspense. The code OP provided under the https://codesandbox.io/s/trusting-turing-widun (PasswordReset is lazy) |
@ljosberinn Sorry, I don't quite understand what you mean by "would work as expected unless he hadn't reimplemented Switch" - the double-negative has confused me! Are you saying it should have worked without reimplementing |
@overlookmotel yes, precisely. Although @MeiKatz is correct in regard that a single Suspense above all routes replaces its children whilst suspended, so with many routes, it matters performance wise. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
If you think there is a React bug please file (in React repo) a new issue with a reduced repro. |
I'm curious about the status of this, and what the recommendations are if one wants to combine Switch with Suspense. I ran into this problem in a current project at work, and I agree that it doesn't act as expected. |
Does this still reproduce with React 17 RC? |
I think so: https://codesandbox.io/s/recursing-ritchie-vvozx?file=/src/App.js navigating to /about and back to / whilst the suspense fallback is shown still waits until loading is finished @whitelizard wrap your routes with |
Sorry I've not produced a reproduction case without React Router. I tried but it got pretty confusing, and... time! I'll give it another go as soon as I get a chance. I had hoped facebook/react#19216 had solved it, but sounds like maybe not. Give me a few days and I'll try to put something together. |
Just tried the original test case with React + ReactDOM 17.0.0-rc.0, and I'm afraid I can confirm the issue remains. I've made a repro case without React Router facebook/react#19701. While this issue remains outstanding in React, might it be worthwhile implementing a workaround in React Router? The problem is the use of This workaround could be implemented so it only kicks in with versions of React which support hooks. Actual implementation would be more complicated, but something along the lines of: const {useContext} = React;
if (useContext) useContext( RouterContext ); |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
This issue remains current as far as I'm aware. No progress has been made with React fixing the bug on their side as yet. facebook/react#19701 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Same deal as on Oct 26th. This bug still remains. |
I know that in React docs it is suggested the other way but what is the downside of putting suspense inside the switch?
|
@ccnklc If I remember right, Also, the location of |
I think we fixed this in facebook/react#23095. Try testing it with the |
Yea this is fixed. https://codesandbox.io/s/react-router-forked-vj12o?file=/index.js |
Thanks, inventor of React! |
right back at ya, creator of react router! |
Isn't the inventor of React Jordan Walke? |
Yes, but the running joke is that people mistake Dan for the creator of React when he actually created Redux (along with Andrew Clark). |
and tim didn’t create react router either! |
@gaearon I know :'D I never heard about that running joke. Next time I will do better in REACTing to this joke ;) |
Version
5.1.2 (not tested on previous versions)
Test Case
https://codesandbox.io/s/react-router-t0ig4
Steps to reproduce
Please see above CodeSandbox.
Expected Behavior
Using React Router with Suspense/lazy.
Home
is a normal component,About
is lazy-loaded.When you click "About" link, "Loading..." should appear (it does).
Now, while
About
is still loading, if you click "Home" link, the page should update immediately and show you the "Home" page again.Actual Behavior
In fact, the route transition back to "Home" is delayed until
About
has finished loading.This is hard to spot in a dev environment, as
About
will load quickly anyway. So in the CodeSandbox above I've substituted a fake Lazy component which never loads. The result is that the page is stuck on "Loading..." forever - navigation ceases to work.Two things solve the problem:
useLocation()
somewhere below the<Switch>
element to force<Switch>
to re-render on a route transition.<Switch>
entirely. If it's just a series of<Route>
elements, it works as expected.I have played around with this a lot, and can't figure out if it's a bug in React Router, or in React's handling of Context within Suspense. But I figured I'd post this here first.
The text was updated successfully, but these errors were encountered: