ReactJS: Component state

ReactJS component's state is set in the constructor, by setting the state property.

You call the super constructor too. The constructor takes in the props passed to the component.

You refer to this by this.state.PROPERTY_NAME.

This is better than than using the this.props directly because you can later use setState to update the component and dependent components using this state.

We'll look at setState next.

    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <div id="root"></div>
  <script type="text/babel">

    class Hello extends React.Component {
      constructor(props) {
        this.state = {
          name: + "!",
      render() {
        return (
          <div style={{display: "flex", flexDirection: "row"}}>
            <div>Hello {}!</div>
            <button>on acid</button>

      <Hello name="Davey dave dave" />,

Edit on github
comments powered by Disqus
Click me