Member-only story

React Events explained, briefly.

MDN
3 min readAug 24, 2020

React by Wikipedia definition (also known as React.js or ReactJS) is an open-source JavaScript library for building user interfaces or UI components.

Many web applications are built now using this JavaScript library. Let’s look at one critical aspect of React: Events.

An event is a user driven JSX callback which can be used to trigger any change in a React Component.

Here is a simplified example taken from the React API:

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')
);

This React component, Toggle has the state isToggleOn, which is declared using the prototype class method. this.isToggleOn is the state of Toggle which changes in the function handleClick.

Within the function handleClick, this.SetState uses this.SetState to change the state of (this.isComponentOn).

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

MDN
MDN

Written by MDN

Composer, Designer, Educator

No responses yet

Write a response