Skip to content

Instantly share code, notes, and snippets.

@nicksheffield
Created June 7, 2019 04:55
Show Gist options
  • Save nicksheffield/6ee6e91d59e5e6458e196e023f273beb to your computer and use it in GitHub Desktop.
Save nicksheffield/6ee6e91d59e5e6458e196e023f273beb to your computer and use it in GitHub Desktop.
Like useReducer, but doesn't trigger component renders
import React, { useRef, useCallback } from 'react'
const useWeakReducer = (reducer, initialState) => {
const ref = useRef(initialState)
const dispatch = useCallback((action) => {
ref.current = reducer(ref.current, action)
}, [])
return [ref.current, dispatch]
}
export default useWeakReducer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment