Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are effects named as "passive effects" in React source code?

As a non native english speaker, i am confused by the term "passive effects", as in schedulePassiveEffects, cancelPassiveEffects in React source code. What exactly does 'passive' mean here?

like image 604
K4ch Avatar asked Jun 27 '26 21:06

K4ch


1 Answers

The reason why it's not just called effect is that there're other Effects in React. There're mutation effects and layout effects along with passive ones.

The passive one is the common one which listens to a state change (through a dependency array) and then can invoke a callback that changes other states.

What's unique about a passive effect is, it waits for all UI (render and commit) to settle down before invoking them in another time slice. So the callback happens in a Javascript time slice similar to an event handler (ex. onClick).

I believe this is where it gets its name, the "passive". Because instead of actively handling an event, it's passively listen to a state change and invoke a callback. I wrote a blog about this, https://javascript.plainenglish.io/is-a-react-passive-effect-an-artificial-event-2535977b9a91

like image 76
windmaomao Avatar answered Jun 29 '26 13:06

windmaomao



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!