Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we add events on react fragment?

In my case, I have to wrap up the content in the parent component without adding extra div element to the DOM. So, I wrapped the element with the react fragment. I got a use case to add the native browser event to the react fragment, Is that even possible?

like image 917
jai sai Kiran Avatar asked Jan 25 '23 11:01

jai sai Kiran


1 Answers

Short answer, no, you can't add events on React Fragments. There is a detailed discussion over here, but as such, I don't think React Fragments will be supporting event handlers in the near future.

You may convert the your React Fragment (from <> </>) to a div element instead, and attach events to them.

return <div onClick={() => handleClick()}> </div>;
like image 158
wentjun Avatar answered Jan 31 '23 13:01

wentjun