Creating Protected Routes With React Router V6

Atefeh
1 min readJul 1, 2022

--

Hey, if you’re looking to find out how to create a Private Route in Reactjs, I’m going to show you how you can do it in only 2 steps. This approach is actually very clean and makes our component much simpler.

The idea is that a user must first meet certain requirements to access that route. In this case, the dashboard page can only be accessed by logged-in users

Step 1

Let’s start by creating a Component, and we call it <PrivateRoute/>. we can pass in more than one route inside of this component. The <Outlet/> allows the Users component to render its child routes.

Step 2

we would simply pass in <PrivateRoute> to a <Route>, and then nest all the components that we want to be private inside of this route.

that’s it 😄, Any developer who is looking forward to learning React or even the ones who have been using React for a while should be able to use and enjoy the stuff in this article!! 😄

if you like this post and it was helpful, please click the clap 👏 button multiple times to show your support, thank you👏

--

--

Atefeh
Atefeh

No responses yet