- When does a React component re-render?
- What does the React.useMemo hook do?
- How can you replicate componentWillUnmount using useEffect ?
- How can you replicate componentDidUpdate using useEffect ?
Created
June 22, 2021 14:26
-
-
Save halitbatur/032fa1617591ba10e892b13075199544 to your computer and use it in GitHub Desktop.
useEffect discussion
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Group Room 7 - M. Zakaria Alalaya, Mohammed Manar Yazji, Ayşe Çimen Başar
The react component re-renders automatically whenever a state or a prop changes / gets updated
add them up, it will just remember the answer is 2 without executing the adding function.
--> const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
componentWillUnmount is a life cycle of React that we use in Class Components and is used to perform clean-up for any DOM-elements.
But with the hooks, we can use life cycle methods in React with function components and we do this with UseEffect
The replication will be as follows :
// With componentWillUnmount()
componentWillUnmount() {
console.log('Hello World');
}
// with useEffect()
useEffect(() => {
console.log('Hello World');
return () => {
console.log('Do some cleanup');
}
}, [])
componentDidUpdate method is invoked immediately after updating occurs.
When we want our state to change when the component renders, we use this method. And we put our state as our initial value in our UseEffect hook.
The replication will be as follows:
// With ccomponentDidUpdate()
componentDidUpdate(prevProps) {
console.log(
Hello World ${prevProps}
);}
// with useEffect()
useEffect(() => {
console.log('Hello World');
}, [prevProps])