Re:Thinking in React Hooks
by emilecantin on Thursday December 12, 2019 @05:10PM
(#21777094)
In this case, onClickOutside was probably a function that you defined inline in your parent component, which would actually be a new thing on each render.
(Something like <Blabla onClickOutsite={() => doSomething()} />)
Since a function is an object, and objects get compared by reference, a new function that does the same thing is not equal to the old function. Thus, this is a change.
Negating it turns that into a boolean, which gets compared by value. false is the same as false, so no change. Any type coercion (e.g turning it into a string) would've worked.
In this case, there's nothing specific to React or hooks, just plain old JS comparison.
Negation just includes the opposite boolean value in the array. _If_ that value at that index happens to be different than the last time the component rendered, the effect will run again.
Re:Thinking in React Hooks
by TheCoelacanth on Friday December 13, 2019 @10:30AM
(#21782404)
The negation is effectively just a cast to boolean in that situation. If the value changes from truthy to not-truthy or vice versa, then the effect will run again, otherwise it won't.
Based on the name, it seem like onClickOutside will always be a function, so always truthy, so it's effectively the same as just leaving it out of the dependency array.
Anyway, I agree with you that at times they can a bit too abstract for the fellow programmer. Without prior knowledge how hooks work it can be quite tedious to read (without proper documentation at least)