The React component lifecycle consists of a series of methods that are invoked at different stages of a component's existence, allowing you to control its behavior from creation to destruction.
The Big Picture
Imagine you’re organizing a stage play. The lifecycle of the play involves various stages: setup (mounting), running the play (updating), and teardown (unmounting). Each stage has specific tasks to ensure the play runs smoothly. Similarly, React components go through mounting, updating, and unmounting phases, each with specific lifecycle methods you can utilize to manage their behavior.
Core Concepts
- Mounting: When a component is created and inserted into the DOM.
- Updating: When a component is re-rendered due to changes in props or state.
- Unmounting: When a component is removed from the DOM.
Detailed Walkthrough
Mounting
Mounting is the phase when a component is created and inserted into the DOM. The main lifecycle methods in this phase are:
- constructor(): Initializes the component's state and binds methods.
- static getDerivedStateFromProps(): Updates the state based on initial props (rarely used).
- render(): Returns the JSX that defines the component’s UI.
- componentDidMount(): Invoked once the component is inserted into the DOM. Ideal for making network requests or initializing libraries.
Example:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
console.log('Constructor');
}
static getDerivedStateFromProps(props, state) {
console.log('getDerivedStateFromProps');
return null;
}
componentDidMount() {
console.log('Component Did Mount');
}
render() {
console.log('Render');
return <h1>Count: {this.state.count}</h1>;
}
}
Updating
Updating occurs when a component's state or props change, triggering a re-render. The main lifecycle methods in this phase are:
- static getDerivedStateFromProps(): Also called during updates to update the state based on props.
- shouldComponentUpdate(): Determines if the component should re-render based on changes in props or state. Returning
false
skips the re-render. - render(): Same as in the mounting phase; returns the JSX.
- getSnapshotBeforeUpdate(): Captures some information (e.g., scroll position) before the DOM is updated.
- componentDidUpdate(): Invoked after the component's updates are flushed to the DOM. Ideal for DOM manipulations or network requests based on prop changes.
Example:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
static getDerivedStateFromProps(props, state) {
return null;
}
shouldComponentUpdate(nextProps, nextState) {
return nextState.count !== this.state.count;
}
getSnapshotBeforeUpdate(prevProps, prevState) {
return null;
}
componentDidUpdate(prevProps, prevState, snapshot) {
console.log('Component Did Update');
}
render() {
return (
<div>
<h1>Count: {this.state.count}</h1>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>Increment</button>
</div>
);
}
}
Unmounting
Unmounting is the phase when a component is removed from the DOM. The main lifecycle method in this phase is:
- componentWillUnmount(): Invoked immediately before the component is removed from the DOM. Ideal for cleanup tasks such as invalidating timers, canceling network requests, or cleaning up subscriptions.
Example:
class MyComponent extends React.Component {
componentWillUnmount() {
console.log('Component Will Unmount');
}
render() {
return <h1>Goodbye, World!</h1>;
}
}
Understanding Through an Example
Consider a timer component that starts counting seconds when it mounts and stops when it unmounts:
class Timer extends React.Component {
constructor(props) {
super(props);
this.state = { seconds: 0 };
}
componentDidMount() {
this.interval = setInterval(() => this.setState({ seconds: this.state.seconds + 1 }), 1000);
}
componentDidUpdate() {
console.log(`Timer updated: ${this.state.seconds} seconds`);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return <h1>Seconds: {this.state.seconds}</h1>;
}
}
Conclusion and Summary
The React component lifecycle consists of three main phases: mounting, updating, and unmounting. Each phase has specific lifecycle methods that allow you to control the behavior of components at different stages of their existence. Understanding these methods helps you manage the component's setup, update, and cleanup tasks efficiently.
Test Your Understanding
- What are the three main phases of the React component lifecycle?
- What lifecycle method would you use to make an API call when the component is first rendered?
- Write a simple component that logs a message to the console every time it updates.
Reference
'400===Dev Library > React' 카테고리의 다른 글
How does the virtual DOM improve performance? (0) | 2024.06.12 |
---|---|
Create a simple React component that displays a message passed as a prop. (0) | 2024.06.12 |
What is a React component, and how does it differ from a regular JavaScript function? (1) | 2024.06.10 |
JSX Introduced (0) | 2024.06.10 |
React Introduced (0) | 2024.05.27 |