#Conditional Routers

1 messages · Page 1 of 1 (latest)

molten solstice
#

Hello, I have mostly just started using the React Router framework for my own projects, before this (in the company I work in) I would mostly use a specialized version of CRA but now I kinda want to learn more frameworks. Here is my problem, In the projects we would conditionally make users use different routers, now the simplest example is NonUser and User but as apps get more complicated and more user types (Admins etc) get introduced it is very useful to be able to allow/disallow pages on router level and not having to do these checks at page level. Is there a way to dynamically change the router/allow disallow routes at the router level?

#

I know I can just use component routes but I wanted to know if there was a way to do it that wasn't so antipattern

harsh bone
#

No. Routes are static at runtime. Ideally you would have middleware for the route trees you want to allow/disallow and redirect users to an allowed page if they try to navigate somewhere they shouldn’t go. This is where you would put the condition.

So to rephrase: the condition isn’t about whether the route exists. It’s about whether a given user can visit the route, and what their experience is if they cannot visit the route.