DEV Community

Discussion on: Quick Tip - Memoizing change handlers in React Components

Collapse
 
dawsonbotsford profile image
Daws Bot

Great article, I think this is mandatory knowledge for intermediate React developers. With the introduction of hooks, how might you add to this example for both memoizing a handler AND avoid recreating that memoized function on each render?

If I'm understanding hooks properly, this tutorial would not work.

Collapse
 
robertbroersma profile image
Robert

Hey Dawson! Thanks for the reply. I realize this might be a bit late, but I think you could use a combination of useCallback and memoize:

const [state, setState] = useState(initialValues)

const handleChange = useCallback(memoize(key => value => {
  setState({ [key]: value })
}), [])

Perhaps there's a better or easier way using useReducer!