I am using a YouTube tutorial to learn React. They are using an older version of react-router-dom
. I have been able to figure out how to use v6
in simple stuff but now I am at PrivateRoutes
. I can't seem to figure out what to change.
# /src/reducers/auth.js
const initialState = {
token: localStorage.getItem('token'),
isAuthenticated: null,
isLoading: false,
user: null
};
export default function(state = initialState, action) {
switch (action.type) {
default:
return state;
}
}
# /src/reducers/index.js
mport { combineReducers } from "redux";
import leads from './leads';
import errors from './errors';
import messages from "./messages";
import auth from "./auth";
export default combineReducers({
leads,
errors,
messages,
auth,
});
// /src/common/PrivateRoute.js
import React from 'react';
import { Route, Navigate } from "react-router-dom";
import { connect } from "react-redux";
import PropTypes from "prop-types";
const PrivateRoute = () => (
// const { user } = useAuth;
<Navigate to='/register' />
)
// const mapStateToProps = state => ({
// auth: state.auth
// });
export default (PrivateRoute);
// export default connect(mapStateToProps)(PrivateRoute);
// App.js
import PrivateRoute from './common/PrivateRoute';
<Routes>
<Route path="/" element={<Dashboard/>} />
<Route path="/login" element={<PrivateRoute><Login/></PrivateRoute>} />
<Route path="/register" element={<Register/>} />
</Routes>
If I don't do anything but use a Navigate
in the PrivateRoute
it works. If I try using ({ children })
I get a lot of errors.
I tried to go through the auth
example on reactor documentation but I think I might be too new to React to understand what I am doing wrong.