Skip to content

Instantly share code, notes, and snippets.

@jmlweb
Created October 8, 2020 11:44
Show Gist options
  • Save jmlweb/dca3bef07eb8d8cfaab6b45004df0393 to your computer and use it in GitHub Desktop.
Save jmlweb/dca3bef07eb8d8cfaab6b45004df0393 to your computer and use it in GitHub Desktop.
import React, { useEffect, useState, memo } from 'react';
const MyComponent = () => {
const [foo, setFoo] = useState('foo');
useMemo(() => {
// componentWillMount
}, []);
useEffect(() => {
// componentDidMount. If we need the DOM => "useLayoutEffect"
fetch('/foo', {
method: 'GET',
})
.then(res => res.json())
.then(json => setFoo(json));
return () => {
// componentWillUnmount
}
}, []);
useEffect(() => {
// componentDidUpdate
}, [foo]);
return null;
}
export default memo(MyComponent); // PureComponent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment