-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added content for component lifecycle method in component-lifecycle.mdx #23
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,63 @@ | ||
--- | ||
title: Component Lifecycle | ||
--- | ||
# Component Lifecycle in React | ||
Understanding the component lifecycle is crucial for managing state and side effects in React applications. This section covers lifecycle methods in class components, the use of the useEffect hook in functional components, and best practices for cleanup. | ||
Lifecycle Methods in Class Components | ||
|
||
React class components have a defined lifecycle that can be divided into three main phases: Mounting, Updating, and Unmounting. Each phase has specific lifecycle methods that allow developers to hook into the component's lifecycle. | ||
## Mounting | ||
The mounting phase occurs when a component is being created and inserted into the DOM. The key lifecycle method in this phase is: | ||
componentDidMount: This method is invoked immediately after a component is mounted. It is commonly used for initializing data, making API calls, or setting up subscriptions. For example: | ||
|
||
componentDidMount() { | ||
fetchData().then(data => this.setState({ data })); | ||
} | ||
|
||
## Updating | ||
The updating phase happens when a component's state or props change, causing it to re-render. The main lifecycle method in this phase is: | ||
componentDidUpdate: This method is called after the component has updated and re-rendered. It receives previous props and state as arguments, allowing developers to compare them and perform actions based on changes. For example: | ||
javascript | ||
componentDidUpdate(prevProps, prevState) { | ||
if (this.props.id !== prevProps.id) { | ||
this.fetchData(this.props.id); | ||
} | ||
} | ||
|
||
## Unmounting | ||
The unmounting phase occurs when a component is being removed from the DOM. The relevant lifecycle method here is: | ||
componentWillUnmount: This method is called just before a component is unmounted and destroyed. Itβs typically used for cleanup tasks such as invalidating timers or canceling network requests. For example: | ||
|
||
componentWillUnmount() { | ||
clearTimeout(this.timer); | ||
} | ||
|
||
## Using useEffect as a Lifecycle Method in Functional Components | ||
With the introduction of functional components and hooks, React provides the useEffect hook to manage side effects similarly to lifecycle methods in class components. | ||
useEffect: This hook can mimic the behavior of componentDidMount, componentDidUpdate, and componentWillUnmount based on how it is used. By default, it runs after every render, but you can control when it runs by passing dependencies. | ||
|
||
Example of using useEffect: | ||
|
||
import { useEffect } from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use astro star light components |
||
|
||
function ExampleComponent({ id }) { | ||
useEffect(() => { | ||
|
||
return () => {// Cleanup function (similar to componentWillUnmount) | ||
cleanup(); | ||
}; | ||
|
||
}, [id]); // Runs when 'id' changes | ||
} | ||
|
||
Handling Cleanup with useEffect | ||
|
||
To prevent memory leaks and ensure proper resource management, itβs essential to implement cleanup functions within useEffect. The cleanup function runs before the component unmounts or before the effect runs again if dependencies change. | ||
Example of implementing cleanup: | ||
javascript | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please provide proper formatting. Also, note that since this doesn't include high-level code contribution we expect great content that can help all levels of developers. Therefore enhance your content with better explanation and code snippets. |
||
useEffect(() => { | ||
const timer = setTimeout(() => { | ||
console.log('Timer executed'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the |
||
}, 1000); | ||
|
||
return () => { | ||
clearTimeout(timer); // Cleanup timer on unmount or before next effect run | ||
}; | ||
}, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be more specific regarding these. Include usage code of the components as well.