I was working on a project and I had to close out some modals, and realized there were both Class component modals and Functional component modals ...
For further actions, you may consider blocking this person and/or reporting abuse
Hi, in your code
handleClickseems to depend onprops.closeAddItemModalwell first of allthis.propsprobably does not work because it's a function component. Secondly ifcloseAddItemModalchanges it probably will cause issues in thisuseEffecthook. It's probably better to add it as a dependency of the effect:OHHH AMAZING THANK YOU GOOD CATCH!
I see you've updated the code. I would move the definition of
handleClickto the body of thatuseEffectfunction (if it is not used elsewhere) so we won't be creating a newhandleClickfunction on each render call. Also it's good to show that we are actually using the dependencies inside the hook directly.If
handleClickis also used in other places, an alternative method to this is to combine this withuseCallbackSo if
props.closeAddItemModaldoesn't change we won't be creating newhandleClickfuncs and ouruseEffecthook will be safe.I'd highly recommend this eslint plugin if you haven't used it npmjs.com/package/eslint-plugin-re...
its only used for this functions, so I just move it above the click listener? :o
Yea I would do that. So the function is within the same block scope created by the effect.
React could render multiple times (with or without any state / prop changes), it's better to keep dependencies and the code where they are used close.
So I made a Util file, and I imported the function and then and callingback to it inside the useEffect, and this works for all my modals except one modal which crashes when I open and close it too many times... any ideas why this could happen?