Categories
React Answers

How to use onClick with divs in React.js?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *