Simulate componentDidMount with a useEffect
If you want to simulate a componentDidMount
class method inside your functional component, you can use the useEffect
hook thusly:
const MyFunctionalComponent = () => {
useEffect(() => {
// any code here is run once
}, []);
// the empty array will be the same each
// time the component is called, so the useEffect
// will not run in calls subsequent to the first
return <div>Foo</div>;
}
ht: @greis
Tweet