You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to create public and private routes in React application while using react-router-dom and createBrowserRouter.
An simple examples, what I would like to have but you can not define createBrowserRouter in the render lifecycle or have conditional routes. What is the solution here for both of those examples?
While I could use loader to have "conditional" rendering but how I would access the context data,
as useSessionUser() is tied to component lifecycle; Is the only solution to use a external store?
What about when I would like to have different index(element) routes in private and public routes?
constUserProvider=({ children })=>{const[user,setUser]=useState(null);const[isLoading,setIsLoading]=useState(true);useEffect(()=>void,[])if(isLoading){returnnull}return<UserContext.Providervalue={user}>{children}</UserContext.Provider>}constApp=()=>{return(<UserProvider>{void 0}</UserProvider>)}
constPRIVATE_ROUTES=void0;constPUBLIC_ROUTES=void0;constFoo=()=>{const{ data, isLoading }=useSessionUser();if(isLoading){return<Loading/>}// ❌ router can not be defined inside render lifecyclesconstrouter=createBrowserRouter(data ? PRIVATE_ROUTES : PUBLIC_ROUTES)return<RouterProviderrouter={router}/>}
constuseBarMutation=()=>{return{mutate: async()=>{constdata=awaitlogin(void0);if(data){updateSessionCache(data);// ❌ we can not navigate to private route from herenavigate("/dashboard")// this route would be defined in PRIVATE_ROUTESreturn}navigate("/");},}}
Important to note:
The createBrowserRouter must be outside the Component render lifecycle and be treated as singleton. Link to maintainer answer
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to create public and private routes in React application while using react-router-dom and
createBrowserRouter
.An simple examples, what I would like to have but you can not define
createBrowserRouter
in the render lifecycle or have conditional routes. What is the solution here for both of those examples?While I could use
loader
to have "conditional" rendering but how I would access the context data,as
useSessionUser()
is tied to component lifecycle; Is the only solution to use a external store?What about when I would like to have different index(element) routes in private and public routes?
Important to note:
createBrowserRouter
must be outside the Component render lifecycle and be treated as singleton. Link to maintainer answerBeta Was this translation helpful? Give feedback.
All reactions