I'm new to ReactJS and I'm trying to understand state and setState(). Using setState() I wanted to change a name, but I am not sure where I should call the setState() method in my code:
- Inside the constructor OR
- Inside the render method OR
- Create a custom method and call it at the end of the constructor before
render()is called
This is my code:
import React from "react";
class StateBasic extends React.Component{
constructor(){
super();
let personProfile = this.state = {
name : "Bob",
skill : "Art Designer",
location : "LA"
}
console.log(personProfile);
}
render(){
let changeName = this.setState({ name : "Frank" });
return(
<div className="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<ul>
<li> {this.state.name} </li>
<li> {this.state.skill} </li>
<li> {this.state.location} </li>
<li> {changeName} </li>
</ul>
</div>
);
}
}
// Let's render ReactDOM
export default StateBasic;