Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Initialise state in class constructor

class App extends React.Component {
  constructor(props) {
    // Required step: always call the parent class' constructor
    super(props);

    // Set the state directly. Use props if necessary.
    this.state = {
      counter: 0,
    }
  }

  render() {
    // whatever you like
  }
}
 
PREVIOUS NEXT
Tagged: #Initialise #state #class #constructor
ADD COMMENT
Topic
Name
7+8 =