The onclick event on the button does not work for the first click, and it works well from the second. Why is this?

state={bank:bankOne};
  handleButtonClick=()=>{
   let button=document.getElementById('button');
   button.style.backgroundColor==='lightcoral'?button.style.backgroundColor='lightgreen':
   button.style.backgroundColor='lightcoral';
   this.state.bank===bankOne? this.setState({bank:bankTwo}):
   this.setState({bank:bankOne});
  }

Is this a javascript challenge?

yes. It is. It’s a react question.

You should not be updating things based on current state outside of this.setState. You need to use the functional version of it.

1 Like

The style does not change for the first click as well. Everything works fines from the second, both the state and the style.

Can you post a link to your project code?