Skip to content

Instantly share code, notes, and snippets.

@DimitarNestorov
Created February 22, 2019 22:47
Show Gist options
  • Save DimitarNestorov/0b14877609597f66e6bb26be6ea06001 to your computer and use it in GitHub Desktop.
Save DimitarNestorov/0b14877609597f66e6bb26be6ea06001 to your computer and use it in GitHub Desktop.
A ref that triggers a rerender https://codesandbox.io/s/3q2nvl3vop
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