I am using React JS and React Router.
I have 2 classes:
- App.js
- SomeClass.js
in App.js:
class App extends React.Component{
constructor(props){
super(props);
this.state = {
token: "",
auth: false
};
}
render(){
return(
<Router>
<Switch>
<Route exact path="/anotherendpoint" component={() => <SomeOtherComponent auth={this.state.auth} token={this.state.token} />} />
<Route path="/someendpoint/abc" exact component={() => <SomeClass auth={this.state.auth} token={this.state.token} />} />
</Switch>
</Router>
);
}
}
export default App;
In SomeClass.js:
class SomeClass extends React.Component{
constructor(props){
super(props);
}
logout = () =>{
console.log('token: ', this.props.token); //getting token: undefined
let myCookie = document.cookie;
console.log("cookie: ", myCookie); //getting cookie: undefined
}
}
export default Nav;
In the SomeClass class, whenever I do this.props.token, I get undefined
Also when I console.log(document.cookie), I get undefined
The problem occurs only in the SomeClass component. What is the reason behind that ?
Why the props are undefined.
Thanks.