bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/React/React Core
React•React Core

React Events

Just like HTML DOM events, React can perform actions based on user events.

React has the same events as HTML: click, change, mouseover etc.

Adding Events

React events are written in camelCase syntax:

onClick instead of onclick .

React event handlers are written inside curly braces:

onClick={shoot} instead of onclick="shoot()" .

React

<button onClick={shoot}>Take the Shot!</button>

HTML

<button onclick="shoot()">Take the Shot!</button>

Example

Put the shoot function inside the Football component:

function Football() {
 const shoot = () => {
 alert("Great Shot!");
 }
 return ( <button onClick={shoot}>Take the shot!</button> );
}
createRoot(document.getElementById('root')).render( <Football /> );

Passing Arguments

To pass an argument to an event handler, use an arrow function.

Example

Send "Goal!" as a parameter to the shoot function, using arrow function:

function Football() {
 const shoot = (a) => {
 alert(a);
 }
 return ( <button onClick={() => shoot("Goal!")}>Take the shot!</button> );
}
createRoot(document.getElementById('root')).render( <Football /> );

React Event Object

Event handlers have access to the React event that triggered the function.

In our example the event is the "click" event.

Example

Arrow Function: Sending the event object manually:

function Football() {
 const shoot = (a, b) => {
 alert(b.type);
 /* 'b' represents the React event that triggered the function, in this case the 'click' event
 */
 }
 return ( <button onClick={(event) => shoot("Goal!", event)}>Take the shot!</button> );
}
createRoot(document.getElementById('root')).render( <Football /> );

This will come in handy when we look at Form in a later chapter.

Previous

React Props Children

Next

React Conditional Rendering

This chapter

Overview
45

Lessons

142m

Read time

1. React Tutorial2. React Introduction3. React Getting Started4. Your First React App5. React Render HTML6. Upgrade React7. React ES68. React ES6 Classes9. React ES6 Arrow Functions10. React ES6 Variables11. JavaScript Array map()12. React ES6 Destructuring13. React ES6 Spread Operator14. React ES6 Modules15. React ES6 Ternary Operator16. React ES6 Template Strings17. React JSX18. React JSX Expressions19. React JSX Attributes20. React JSX If Statements21. React Components22. React Class Components23. React Props24. React Destructuring Props25. React Props Children26. React Events27. React Conditional Rendering28. React Lists29. React Forms30. React Submit Forms31. React Forms - Textarea32. React Forms - Select33. React Forms - Multiple Input Fields34. React Forms - Checkbox35. React Forms - Radio36. React Portals37. React Suspense38. Styling React Using CSS39. React CSS Modules40. React CSS-in-JS41. React Router42. React Transitions43. React forwardRef44. React Higher Order Components45. React Sass Styling

On this page

Adding EventsPassing ArgumentsReact Event Object