I am using React and React Router.
Here is my first component:
import React from 'react';
class SomeClass extends React.Component{
constructor(props){
super(props);
this.state = {
someObj: {
something: "",
someOtherThing: ""
},
authenticated: false
};
}
}
export default SomeClass;
Here is my ProtectedRoute component: const ProtectedRoute = ({auth, component: Component, ...rest}) =>{ return( <Route {...rest} render={(props) =>{
if(auth){
return(<Component {...props} />);
}
if(!auth){
return(<Redirect to={{path: "/", state:{from: props.location}}} />);
}
}} />
);
};
export default ProtectedRoute;
Here is my Nav component:
import React from 'react';
import { BrowserRouter as Router, Switch} from 'react-router-dom';
import ProtectedRoute from './ProtectedRoute';
import SomeClass from './SomeClass';
function Nav(){
return(
<Router>
<Switch>
<ProtectedRoute exact path="/somepage/abc" component={SomeClass} auth={SomeClass.props.authenticated} /> //getting an error here because of SomeClass.props.authenticated
</Switch>
</Router>
);
}
export default Nav;
My Question:
In my Nav functional component, there is the <ProtectedRoute ....>, I want to get the authenticated value from the SomeClass's authenticated value (this.state.authenticated)?
When I do SomeClass.props.authenticated I get the following error:
TypeError: Cannot read property 'authenticated' of undefined