Answers for "how to write onclick in react"

0

onclick inline function react

//Inline onclick function
<button onClick={(e) => console.log(e)}>
        Click me
</button>
Posted by: Guest on January-28-2021
0

handleClick react

class Toggle extends React.Component {
  constructor(props) {
    super(props);
    this.state = {isToggleOn: true};

    // This binding is necessary to make `this` work in the callback    this.handleClick = this.handleClick.bind(this);  }

  handleClick() {    this.setState(state => ({      isToggleOn: !state.isToggleOn    }));  }
  render() {
    return (
      <button onClick={this.handleClick}>        {this.state.isToggleOn ? 'ON' : 'OFF'}
      </button>
    );
  }
}

ReactDOM.render(
  <Toggle />,
  document.getElementById('root')
);
Posted by: Guest on January-30-2021

Code answers related to "how to write onclick in react"

Code answers related to "Javascript"

Browse Popular Code Answers by Language