feat: support loader/action requestContext on the client #10895
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Addresses this request: #9856
This change allows the end user to pass requestContext into the browser router, just like they can when calling the static router query handlers. Not sure if ya'll are willing to consider this change (or if where I put the options makes sense, etc), but it was simple so I figured I'd open it. Or if that middleware proposal is being worked on and supersedes this.
It doesn't involve React.Context, nor does it require rendering.
It allows for isomorphic data loader functions. A good example is the react-query use case - on the server the queryClient needs to be created per request and passed in (can't do singleton, so the existing static router requestContext functionality is perfect for this). However, then we're left with loaders that are broken on the client, since they're expecting queryClient in the requestContext object. With this change, we can now pass the queryClient in when creating the browser router.
Example react-query use case:
I'm not very familiar with the react-router codebase, so I may have missed some areas that need further adjustment.