This tutorial shows how to create Private Route in React using hooks.

Prerequisites:

  • React js
  • Hooks
  • React Router

Step 1: Start by creating a react app.

Step 2: Install react router dom.

npm install react-router-dom

Step 3: Lets create a reusable auth component

  • The auth component will return 2 things : Sign in status & User.
  • In Below code use your API to get the user and sign in status.

Step 4: Lets create our private route

Step 4: How to use it

  • Go to your file where all routes exists
  • Import private route
  • Pass path and component you want to render

That’s it.

Thanks for reading.

--

--