Skip to content

Instantly share code, notes, and snippets.

@afahy
Created May 19, 2020 12:44
Show Gist options
  • Save afahy/59ecfd86b272ccf0179da8ca2a60a298 to your computer and use it in GitHub Desktop.
Save afahy/59ecfd86b272ccf0179da8ca2a60a298 to your computer and use it in GitHub Desktop.
import React from 'react';
export default function useUpdateEffect(callback, properties, cleanup) {
let initialized = React.useRef(false);
React.useEffect(() => {
if (initialized.current) {
callback(properties);
} else {
initialized.current = true;
}
return () => {
if (typeof cleanup === 'function') {
cleanup();
}
}
}, properties);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment