Skip to content

react-hooks/exhaustive-deps warnings when using useMemo to try and prevent unnecessary re-rendering of the controls. #393

Answered by dbismut
Sean-Bradley asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @Sean-Bradley, a few observations:

  • what's the problem with buttons being recreated on each render? Is there some scaling issue that I'm not aware of?
  • there's no problem with bypassing the exhaustive-deps lint rule in general, as long as you know what you're doing. We actually do that a lot in Leva 🤧

However, here the solution to your problem is pretty obvious:

 const buttons = useMemo(() => {
     const _buttons = {}
    positions.forEach((p, i) => {
      _buttons['button ' + i] = button(() => setTo(p))
    })
   return _buttons
  }, [positions, setTo])

And if you're a fan of the reduce function:

 const buttons = useMemo(() => positions.reduce((acc, p, i) =>
  Object.assign(acc, { [`…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Sean-Bradley
Comment options

@dbismut
Comment options

Answer selected by Sean-Bradley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #392 on October 14, 2022 19:06.