chevron-up bell reply instagram twitter2 feed3 finder search-25px-p0

Get and Set a state in React

2017-01-06

The state is a JavaScript object that lives inside each of the component, We can aaccess it via this.state. Unlike JQuery and Backbone, DOM is manipulated indirectly in React, which means we don't modify the DOM directly.

Use Scenarios

  • Button click
  • Link click

How to use state

1. Set the Component's state in the constructor

class Comment extends React.Component {

  constructor(){
    super();
    this.state = {
        isAbusive:false,
    };
  }
  ...
}

2. Update the Component's state via setState()
We don't update the state directly.

this.setState({isAbusive:false})

A full example:

Reference

arkilis

Make Your Comments