Skip to content

Instantly share code, notes, and snippets.

@lpolito
Last active February 7, 2020 14:10
Show Gist options
  • Save lpolito/b7bfb58f8c5bd698a35e1fd364d1df95 to your computer and use it in GitHub Desktop.
Save lpolito/b7bfb58f8c5bd698a35e1fd364d1df95 to your computer and use it in GitHub Desktop.
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