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: Wrap route with location arg in location context #9094

Merged
merged 5 commits into from
Sep 12, 2022
Merged

fix: Wrap route with location arg in location context #9094

merged 5 commits into from
Sep 12, 2022

Conversation

johnpangalos
Copy link
Contributor

@johnpangalos johnpangalos commented Jul 25, 2022

In order for the useLocation hook to work with the use of the modal pattern, routes with the locationArg prop are wrapped in a LocationContext. This is done conditionally in order to ensure performance in the default case doesn't change.

closes #8470

@changeset-bot
Copy link

changeset-bot bot commented Jul 25, 2022

🦋 Changeset detected

Latest commit: 29f273b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
react-router Patch
react-router-dom Patch
react-router-dom-v5-compat Patch
react-router-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@johnpangalos
Copy link
Contributor Author

Not sure how changesets are meant to work, they aren't mentioned in contributing document but the changeset bot has kindly reminded me about them.

@johnpangalos
Copy link
Contributor Author

I also bumped the file sizes half a kB as it seems that the code I added is now breaking that limit. Is there any guidance as what should be done when reaching that limit?

@timdorr timdorr requested a review from brophdawg11 July 25, 2022 16:23
@timdorr
Copy link
Member

timdorr commented Jul 25, 2022

For the changesets, just follow the links in the comment above. This change should result in a patch version bump (it fixes a bug), so I would add one.

As for the size thing, that's fine to bump up just a little. It's mainly a protection against adding something stupidly huge in a PR without anyone noticing.

@johnpangalos johnpangalos changed the title Wrap route with location arg in location context fix: Wrap route with location arg in location context Jul 26, 2022
@brophdawg11
Copy link
Contributor

Thanks for the PR @johnpangalos! I left a comment over on #8470 but I'll ping Michael and Ryan on this as well to get their take

In order for the `useLocation` hook to work with the use of the modal
pattern, routes with the locationArg prop are wrapped in a
LocationContext. This is done conditionally in order to ensure
performance in the default case doesn't change.
@flying-sheep
Copy link

@mjackson, @ryanflorence, could you please take a look? I think a lot of people are eagerly waiting for this fix!

@brophdawg11
Copy link
Contributor

Thanks @johnpangalos! Made a few small updates, but this looking good and I'll merge shortly

@brophdawg11 brophdawg11 merged commit e766ab5 into remix-run:dev Sep 12, 2022
@johnpangalos
Copy link
Contributor Author

Sounds good, glad to see this merged! Thanks @brophdawg11

@gnapse
Copy link

gnapse commented Aug 7, 2024

Hi, there's something about this change that I wanted to ask about: having this bifurcation in the code, rendering two different component trees depending on whether there's a location prop provided or not, has a troublesome implication: the entire tree inside the routes is re-built. Not merely re-rendered, but components are discarded, and new ones are mounted.

To illustrate, check at this short screen recording where I show how the component tree re-rendered in full, because the tree change after having introduced a Location.Provider in between the parent Routes and the child RenderedRoute:

CleanShot.2024-08-07.at.14.19.29.mp4

Note how before the rendering commit took place, the tree starts with Routes -> RenderedRoute, and after the commit took place, the structure is now Routes -> Location.Provider -> RenderedRoute. This causes RenderedRoute and everything below it to not merely update, but to be unmounted, and re-created entirely. This in turn causes elements to be re-created, which can cause state in them to be lost.

This is understandable due to how React reconciliation works. But it is undesirable, due to the potential to lose inner component state in this entire sub-tree.

Proposed solution

What if we always wrap the return value of useRoutes inside a Location.Provider?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: useLocation inside <Routes location={location}> doesn't return given location
5 participants