Last active
February 7, 2020 14:10
-
-
Save lpolito/b7bfb58f8c5bd698a35e1fd364d1df95 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 React from 'react'; | |
// Initialize a ref once during init to prevent something being redefined on every render. | |
export const useLazyInit = (value) => { | |
const valueRef = React.useRef(null); | |
if (valueRef.current === null) { | |
valueRef.current = typeof value === 'function' ? value() : value; | |
} | |
return valueRef.current; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment