I am learning react-router protected routes. I am setting up a page with just one button that can sign in and sign out a fake user.
If I try to visit the path "/" in the root component below, then I should just see a "Sign In" button. Except I only see the first two console logs and then the error.
The error looks like as follows
DevTools failed to load source map: Could not load content for webpack://parier/node_modules/react-router/esm/react-router.js.map: Fetch through target failed: Unsupported URL scheme; Fallback: HTTP error: status code 404, net::ERR_UNKNOWN_URL_SCHEME
Here is my Root.js
const [user, setUser] = React.useState(null);
const handleLogin = () => setUser({
id: '2',
name: "Temporary Marker",
isAuthenticated: true
});const handleLogout = () => setUser(null);
console.log("<ROOT>: User before render is: ");
console.log(user);
return(
<div className="site">
<Switch>
<Route exact path={"/demo/"} component={Demo}/>
<Route exact path={"/home/"} component={Home}/>
<Route element={<ProtectedRoute user={user} />}>
<Route path="play" element={<Play />} />
</Route>
<Route path={"/"} render={() =>
<div>
Default page
<div>
{user ? (
<button onClick={handleLogout}>Sign Out</button>
) : (
<button onClick={handleLogin}>Sign In</button>
)}
</div>
</div>
}
/>
<Route path="*" element={<p>There's nothing here: 404!</p>} />
</Switch>
</div>
)
}
export default Root;
and here is the ProtectedRoute.js component
import react from 'react';
import { Routes, Route, Link} from 'react-router-dom';
const ProtectedRoute = ({ user, redirectPath = '/', children }) => {
if (!user) {
console.log("<PROTECTEDROUTE>: User is not defined. Redirecting to / path");
return <Link to={redirectPath} replace />;
}
return children;
};
export default ProtectedRoute;
What am I doing wrong?
Thanks