Skip to content

createBrowserRouter inside component #10440

Closed Answered by brophdawg11
kristersd asked this question in Q&A
Discussion options

You must be logged in to vote

We should probably make this more clear in the docs (PR's welcome!) but the intention is that your router is a singleton for your application as shown in the docs. If you are doing it inside a react component then you end up creating a new router anytime that component re-renders which will blow away any old state of the router.

Instead, you can setup or create contexts in your App component or a root layout component, and then access them from inside your route components. You cannot leverage context in loaders and actions since that breaks the decoupling of fetching and rendering which is at the core of the 6.4+ data API design.

Replies: 4 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@kristersd
Comment options

Comment options

You must be logged in to vote
1 reply
@User6531
Comment options

Answer selected by kristersd
Comment options

You must be logged in to vote
2 replies
@brophdawg11
Comment options

@aezing
Comment options

Comment options

You must be logged in to vote
1 reply
@brophdawg11
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants