The goal from this discussion is to solidify your understanding about useEffect after studying about it before the class and in the class.
Please discuss the following questions between you and answer them in the comment section below.
Questions:
- What is
useEffect
? - What do we mean when we say that useEffect is a hook?
- useEffect gets executed in 3 different ways, what are they? (Adding code examples would be beneficial)
@sakhile Motha
@Vuyo Ngwane
1)useEffect is a React Hook that allows you to perform side effects in functional components. Side effects are operations that happen outside the regular rendering lifecycle of a React component
2)We mean that it's a special function that lets you perform side effects in your functional components, such as fetching data, directly interacting with the DOM, or setting up subscriptions. Hooks like
useEffect
enable functional components to use features that were previously only available in class components, making it easier to manage component logic and state.3)No Dependency Array: useEffect runs after every render when you don't provide a dependency array.
useEffect(() => {
console.log();
});
*Empty Dependency Array: useEffect runs only once after the initial render when you provide an empty dependency array.
useEffect(() => {
console.log();
}, []);
useEffect(() => {
console.log();
}, [count]);