Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preventDefault vs return false when it comes to reactjs

I started learning Reactjs today and found an interesting fact (and din't find any relevant topic) that we must use preventDefault than using return false statement to prevent the default behavior: https://reactjs.org/docs/handling-events.html

But as far as I know, return false is equivalent to (from jQuery background):

// e.preventDefault();
// e.stopPropagation();
// stop function execution after return false
// stops the callback function

But I'm curious to know here how react has implemented so that we cannot use return false statement and we must use preventDefault to prevent the default behavior.

So, what's exactly difference between preventDefault and return false when it comes to reactjs? Why preventDefault is more robust than return false in reactjs?

like image 737
Bhojendra Rauniyar Avatar asked May 03 '26 18:05

Bhojendra Rauniyar


1 Answers

The events in react are not the native events of the browser, they are wrapped for your convenience.

Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper around the browser’s native event. It has the same interface as the browser’s native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.

https://reactjs.org/docs/events.html

So when you do preventDefault, you are not calling the method of the native event directly, but instead, react takes care of you.

So returning false is returning false to reacts event and not the native event one, meaning, it doesn't really do anything...

like image 60
Yftach Avatar answered May 06 '26 07:05

Yftach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!