useConstant is a custom hook that creates a constant value over the lifecycle of a component. Even if useMemo is provided an empty array as its final argument, it doesn't offer a guarantee that it won't re-run for performance reasons later on. By using useConstant you can ensure that initialisers don't execute twice or more.


import { useConstant } from '@dwarvesf/react-hooks'

Return value#

The useConstant hook returns a function that receives the element and assign the value to the given React refs.


function Example() {
const value = useConstant(() => {
// heavy computation
return 0
return <p>{value}</p>


The useConstant hook accepts a single value param:

valueany | Function_The constant value.
Edit this page

Made with ❤️ by Dwarves Foundation