Explain setState() Method in reactjs?

Answer

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>
);
}
}

All react js Questions

Ask your interview questions on react-js

Write Your comment or Questions if you want the answers on react-js from react-js Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---