• Home
  • >
  • Tech News
  • >
  • React onClick Event Handling – Detailed tutorial

Events can be handled in React quite similar to the way we do in HTML, these events be like click, change, mouseover, etc. But ‘React events’ are written in camelCase syntax.

For example: “onClick”instead of “onclick” and the function which this event point to would be written in curly braces, for example: onClick={function}.

In this blog, we will talk only about ‘onClick‘ event handling. The onClick handler allows you to call a function and perform an action when an element is clicked.

Let’s learn about this with an example

Create a new project by the following command:

npm create-react-app reactOnClick

where, reactOnClick is the project name.

Now, go to the project directory and start it-

npm start

You should have your project started in the localhost.

Now, in src/App.js, add the following code:

import React from 'react';
function App() {
const testClick = () => {
alert('Hey, you just clicked me');
return (
<div className="App">
<button onClick={testClick}>
Click me!
export default App;

In this, we have a button which when clicked calls the function “testClick” ,which alerts the user that he clicked the button.

You will see something like the following after clicking the button

Some common mistake people do is that they pass the function to the handler with parentheses like:

<button onClick={testClick()}>

what it would do is, call the “testClick” function on every render without even clicking it.

It is not necessary to call a function with a name, we can also call inline functions just like:

<button onClick={() => alert('Hey, you just clicked me')}>

So now you must be thinking, if we cannot pass the function name with parentheses then how can we pass arguments to a function.

It can be easily done with an inline function calling that function.

To be more clear, see the following code:

function App() {
const testClick = (name) => {
alert('Hello ' + name);
return (
<div className="App">
<button onClick={() => testClick('John')}>
Click me!

You will see the following in your browser:

Read More:   Update Blockchain Hyperledger Project Generates Interest in the Enterprise

Similarly, the states can be managed by ‘onClick’ events-

import React, {useState} from 'react';
function App() {
const [count, setCount] = useState(0);
return (
<div className="App">
<h1>Button is clicked {count} times</h1>
<button onClick={() => setCount(prevState =>
prevState + 1)}>
Click me!
export default App;

And in the browser, we have-

So, Event handlers determine what action should be taken when an event occurs. The “onClick” event is used to listen for click events “onDOM” elements.

Hope you all have a clear picture about ‘onClick’ event handling. Will be sharing more such concepts in my future posts.

Source: InApps.net

List of Keywords users find our article on Google:

scrum tutorial
wawa app
div onclick
onclick function
html onclick
js onclick
react classname
js button onclick
onclick js
braces el segundo ca
linkedin tutorial
onclick event
button onclick javascript
javascript button onclick
devops tutorial point
case handler jobs
a onclick
onclick alert
a onclick html
telegram inline button
html div onclick
react recruitment
circleci react app
clear braces el segundo ca
react onclick not working
wawa delivery
onclick function js
onclick html
input onclick
circleci vs
ats specs
circleci status
toby nguyen
vscode react
react const or function
call center offshore outsourcing
js mobile click event
react button events
onclick javascript
javascript on click
npm alert
div onclick react
on click div
react onclick div
npm react-select
on click javascript
button onclick
onclick in html
button onclick html
react vscode shortcuts
html a onclick
npm event
html button onclick
element onclick
onclick button js
event pass template
react shortcuts vscode
scrum hcm
tam tutorial
hcmc info on call
listen to button click js
button onclick in html
prevstate react
react select onclick
ats new project
how to call a function on button click in python
elementor events
function or const react
classname react
prevstate usestate
react const function
react in vscode
react js button
react return div
button onclick event
react function syntax
function app react
react pass function
circleci npm
react e-commerce
circleci office
scrum tutorials
react remote developer
react outsourcing
Rate this post

Let’s create the next big thing together!

Coming together is a beginning. Keeping together is progress. Working together is success.

Let’s talk

Get a custom Proposal

Please fill in your information and your need to get a suitable solution.

    You need to enter your email to download


      Success. Downloading...