Which lifecycle methods of class component is replaced by useEffect in functional component?
Which lifecycle methods of class component is replaced by useEffect in functional component?
The useEffect
hook in React functional components replaces several lifecycle methods of class components. Specifically, it serves as a replacement for the following lifecycle methods:
componentDidMount
: This lifecycle method is used in class components to perform actions after the component has been inserted into the DOM. In functional components, useEffect
with an empty dependency array ([]
) mimics this behavior, running only once after the initial render[1][2][3][4].
componentDidUpdate
: In class components, this method is invoked after updates to the component, such as changes in state or props. useEffect
can replicate this by including specific dependencies in its dependency array. When any of these dependencies change, the effect runs again, thus mimicking the componentDidUpdate
method[1][2][3][4].
componentWillUnmount
: This method is used for cleanup activities when a component is about to be removed from the DOM. useEffect
handles this through a return function that specifies cleanup actions. This function is executed when the ...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào