How to prevent certain users from accessing a route and any subroutes in React?
问题 What's the best way to prevent a particular user from accessing a route and any of its subroutes in react-router ? e.g. I have a route /admin/ and it has many other subroutes like /admin/create-user , /admin/settings , etc. Is there another way of preventing users from accessing these routes without indicating on each component some condition like so? if (user.role.name !== "Admin") { return ( <div className="error-page"> <h1>Sorry, you don't have rights to access this page.</h1> </div> ); }