Package Exports
- @rooks/use-fresh-ref
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@rooks/use-fresh-ref) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@rooks/use-fresh-ref
About
Avoid stale state in callbacks with this hook. Auto updates values using a ref.
Installation
npm install --save @rooks/use-fresh-ref
Importing the hook
import useFreshRef from "@rooks/use-fresh-ref"
Usage
function Demo() {
const [value, setValue] = useState(5)
function increment(){
setValue(value + 1)
}
const freshIncrementRef = useFreshRef(increment);
useEffect(() => {
function tick(){
freshIncrementRef.current();
}
const intervalId = setInterval(tick,1000)
return clearInterval(intervalId)
}, [])
return null
}
render(<Demo/>)