Created
February 22, 2019 22:47
-
-
Save DimitarNestorov/0b14877609597f66e6bb26be6ea06001 to your computer and use it in GitHub Desktop.
A ref that triggers a rerender https://codesandbox.io/s/3q2nvl3vop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useCallback } from 'react'; | |
function useStateRef(initialValue) { | |
const [ref, setState] = useState(initialValue) | |
const callback = useCallback( | |
ref => { | |
setState(ref) | |
}, | |
[ref, setState] | |
) | |
callback.current = ref | |
return callback | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment