How to use onClick with divs in React.js?

Sometimes, we want to use onClick with divs in React.js.

in this article, we’ll look at how to use onClick with divs in React.js.

How to use onClick with divs in React.js?

To use onClick with divs in React.js, we set the onClick prop to a function reference.

For instance, we write

<div onClick={doThis}>...</div>

or

<div onClick={() => doThis()}>...</div>

to set the onClick prop to a function.

We set it to the doThis function directly in the first example.

And we set it to a function that calls doThis in the 2nd example.

Conclusion

To use onClick with divs in React.js, we set the onClick prop to a function reference.