Skip to content
Advertisement

Error: [PrivateRoute] is not a component. All component children of must be a or

I’m using React Router v6 and am creating private routes for my application.

In file PrivateRoute.js, I’ve the code

JavaScript

And in file route.js I’ve written as:

JavaScript

I’ve gone through the same example React-router Auth Example – StackBlitz, file App.tsx

Is there something I’m missing?

Advertisement

Answer

I ran into the same issue today and came up with the following solution based on this very helpful article by Andrew Luca

In PrivateRoute.js:

JavaScript

In App.js (I’ve left in some other pages as examples):

JavaScript

In the above routing, this is the private route:

JavaScript

If authorization is successful, the element will show. Otherwise, it will navigate to the login page.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement