Does "thinking in lifecycle" still a thing given that we all use functional components nowadays?
How to use lifecycle methods with hooks in React?
Am i the only one who finds React lifecycle hooks super confusing?
reactjs - React Hooks and React lifecycle Methods - Stack Overflow
Videos
Just got two interviews and they all asked about this stuff. I bombed it because my knowledge about class component and lifecycle methods is really limited (only remember one component can mount, update and unmount but don't know about functions like componentWillMount and stuff)
With the new React docs, I can't find any instance of lifecycle other than "Lifecycle of an effect". So I don't really know if I am missing out one of the most fundamental pieces of React or it's just a question that interviewers like to ask...
Here are examples for the most common lifecycles:
componentDidMount
Pass an empty array as the second argument to useEffect() to run only the callback on mount only.
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
}, []); // Pass an empty array to run only callback on mount only.
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
componentDidUpdate (loose)
By passing just the single argument into useEffect, it will run after every render. This is a loose equivalent because there's a slight difference here being componentDidUpdate will not run after the first render but this hooks version runs after every render.
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
}); // No second argument, so run after every render.
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
componentDidUpdate (strict)
The difference of this example with the example above is that the callback here would not run on initial render, strictly emulating the semantics of componentDidUpdate. This answer is by Tholle, all credit goes to him.
function Example() {
const [count, setCount] = useState(0);
const firstUpdate = useRef(true);
useLayoutEffect(() => {
if (firstUpdate.current) {
firstUpdate.current = false;
return;
}
console.log('componentDidUpdate');
});
return (
<div>
<p>componentDidUpdate: {count} times</p>
<button
onClick={() => {
setCount(count + 1);
}}
>
Click Me
</button>
</div>
);
}
componentWillUnmount
Return a callback in useEffect's callback argument and it will be called before unmounting.
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
// Return a callback in useEffect and it will be called before unmounting.
return () => {
console.log('componentWillUnmount!');
};
}, []);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
shouldComponentUpdate
You can already achieve this on the component level using React.PureComponent or React.memo. For preventing rerendering of the child components, this example is taken from React docs:
function Parent({ a, b }) {
// Only re-rendered if `a` changes:
const child1 = useMemo(() => <Child1 a={a} />, [a]);
// Only re-rendered if `b` changes:
const child2 = useMemo(() => <Child2 b={b} />, [b]);
return (
<>
{child1}
{child2}
</>
)
}
getDerivedStateFromProps
Again, taken from the React docs
function ScrollView({row}) {
let [isScrollingDown, setIsScrollingDown] = useState(false);
let [prevRow, setPrevRow] = useState(null);
if (row !== prevRow) {
// Row changed since last render. Update isScrollingDown.
setIsScrollingDown(prevRow !== null && row > prevRow);
setPrevRow(row);
}
return `Scrolling down: ${isScrollingDown}`;
}
getSnapshotBeforeUpdate
No equivalent for hooks yet.
componentDidCatch
No equivalent for hooks yet.
Well, you don't really have lifecycle methods. =) But you could use the effect hook as shown here https://reactjs.org/docs/hooks-effect.html
The effect hook will be able to replicate the behavior of componentDidMount, componentDidUpdate, and componentWillUnmount
So you really don't need lifecycle methods in the component. The effect hook is taking their place instead. =)
Read the link above and you will get a few examples on how they work.
It's been a year since I started learning web dev, javascript, react js and everytime i go through a react tutorial or guide, i get confused with the lifecycle hooks.
I have worked on a few small React projects and I still don't know how to incorporate lifecycle hooks in to the projects. Is their a super simple guide that demonstrates how the hooks work in real life scenarios?