As the official docs say componentWillMount
is deprecated now and is recommended to put
any code that is meant for this lifecycle method into the constructor.
Honestly I don't know how to do that. I've got this code that was meant for componentWillMount
but how I'm supposed to implement it into the constructor:
if (window.localStorage.getItem("authToken"))
this.setState({ isAuthenticated: true });
I had it like this:
constructor() {
super();
this.state = {
users: [],
username: "",
email: "",
title: "something",
isAuthenticated: false
};
if (window.localStorage.getItem("authToken"))
this.setState({ isAuthenticated: true });
}
but the condition wasn't triggered when it was supposed to trigger. How are conditional statements supposed to work in a constructor ?
Any guidance much appreciated.
Edit:
constructor() {
super();
this.state = {
users: [],
username: "",
email: "",
title: "something",
isAuthenticated: window.localStorage.getItem("authToken") ? true : false
};
}
I'll try this as this does make sense to me.