Hello! Nice to see you again! If you are studying or starting to work with ReactJS, you have probably heard about the Event Handling. If you have a question about what it is and how to use it, then read on. In this article, I will explain to you “How to use onClick Events in ReactJS”. Let’s start from the beginning.
1What is the React onClick Event Handler?
ReactJS uses the button click event, which is set via the onClick attribute. The “this.press” function, which is defined in the component class, was passed to this attribute as an event handler. And when the button is pressed, the press function will be called. The main difficulty that can arise when using events is working with this keyword, which points to the current object, in this case, a component. By default, the current object is not passed to the handler function, so this will be undefined. And we cannot refer to any properties and methods of the component through this. React uses the concept of SyntheticEvent – special objects that are wrappers for event objects passed to an event function. And using such an object, we can get all the information about the event in the event handler.
Let’s look at some examples of how we can use the onClick event handler in React:
- Call a Function After Clicking a Button: The simple App component has one function called sayHello(), and a single button. The button inside the React component has an onClick event handler attached to it, pointing to our sayHello() function. Doing so will trigger the function every time you press the button. How do we trigger the sayHello() function? Bypassing in the name of the function you want to call inside the curly braces, after the event handler. Notice how we only pass the name of the function to the event handler and not the name followed by parentheses. Parentheses after a function name execute the function. In other words, if we passed in the function with parentheses, the function will execute every time the component renders. We do not need to execute a function inside of the event handler.
- Call an Inline Function in an onClick Event Handler: An inline function is a function that is defined inside of the onClick handler when the React Component renders. It’s caused on render because the function definition is inside of the onClick handler, which is inside of the component render method (or return, in the case of functional React components). You’d use an inline function to pass in additional arguments to a function, such as a value from a loop, or the target button’s value.
- Call Multiple Functions in the onClick Event Handler: Maybe you would like to call multiple functions after clicking a button. For example, updating a component’s state and simultaneously showing an alert. There are two ways to do this: 1) Running a block of code inside the onClick handler; 2) Calling multiple functions inside the onClick handler.
- Updating the State in the onClick Event Handler: A very common use of an inline function inside of the onClick event handler in React is to update a component’s state. You’ll do this when you want to update the state with the button’s value, or using a value from a loop, for example.
Event handlers are used to determine what action is to be taken whenever an event is fired. This could be a mouse click or a change in a text input. In React apps, events are written in the CamelCase format, which means the onClick event will be written as onClick in a React app. The onClick event is used to listen for click events on DOM elements, to call a function and perform an action. You also saw examples in which you can use the onClick Event Handler in ReactJS. Hope you enjoyed it and received enough information!