setState() method is used for changing state objects, components will be updated and call the render() method.
setState() method enqueues all of the updates made to the components and instruct react to re-render the component and
its childern within the updated state.
Example - class Bike extends React.Component {
constructor(props) {
super(props);
this.state = {
make: "Yamaha",
model: "R15",
color: "blue"
};
}
changeBikeColor = () => {
this.setState({color: "black"});
}
render() {
return (
<div>
<h2>My {this.state.make}</h2>
<p>
It is a {this.state.color}
{this.state.model}.
</p>
<button
type="button"
onClick={this.changeBikeColor}
>Change color</button>
</div>
);
}
}