React changing state

Why doesn’t this code work?

  render() {

    
   return (
      <div className="App container">
        <button className="btn btn-primary mt-4" onClick={changeState}>Working</button>
        <div className="myState">
          <p>This state of my app {this.state.myState}</p>
        </div>
      </div>
    );
  }


  changeState(){
    this.setState({myState: 'working'})
 }

}

export default App;

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

Can’t remember off the top of my head, so excuse me if I’m wrong, but shouldn’t the call to changeState in the onClick atrribute be this.changeState?

onClick={this.changeState}
1 Like