Basic State
suggest changeState in React components is essential to manage and communicate data in your application. It is represented as a JavaScript object and has component-level scope, and it can be thought of as the private data of your component.
In the example below we define some initial state in the component’s constructor and use it in render.
class ExampleComponent extends React.Component {
constructor(props){
super(props);
// Set up our initial state
this.state = {
greeting: 'Hiya Buddy!'
};
}
render() {
// We can access the greeting property through this.state
return(
<div>{this.state.greeting}</div>
);
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents