Flat Preloader Icon

Using Lifecycle Methods

Using lifecycle methods in React involves understanding how and when to apply them to manage the different stages of a component’s existence. Here’s a detailed guide on how to use lifecycle methods effectively in class components and how to achieve similar functionality with hooks in functional components.

Lifecycle Methods in Class Components

1. Mounting

  • constructor(props): Initialize state and bind event handlers.
				
					import React from 'react';

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState({ count: this.state.count + 1 });
  }

  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Increment</button>
        <p>Count: {this.state.count}</p>
      </div>
    );
  }
}

				
			
  • static getDerivedStateFromProps(props, state): Update state based on changes in props before rendering.
				
					import React from 'react';

class MyComponent extends React.Component {
  static getDerivedStateFromProps(nextProps, prevState) {
    if (nextProps.value !== prevState.value) {
      return { value: nextProps.value };
    }
    return null;
  }

  render() {
    return <p>Value: {this.state.value}</p>;
  }
}

				
			
  • render():Define the UI structure of the component.
				
					import React from 'react';

class MyComponent extends React.Component {
  render() {
    return <div>Hello, world!</div>;
  }
}

				
			
  • componentDidMount():Perform side-effects like data fetching or subscriptions.
				
					import React from 'react';

class MyComponent extends React.Component {
  componentDidMount() {
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(data => this.setState({ data }));
  }

  render() {
    return <div>Data: {this.state.data}</div>;
  }
}

				
			

2. Updating

  • static getDerivedStateFromProps(props, state):Same as in mounting.
  • shouldComponentUpdate(nextProps, nextState):Optimize performance by preventing unnecessary re-renders.
				
					import React from 'react';

class MyComponent extends React.Component {
  shouldComponentUpdate(nextProps, nextState) {
    return nextProps.value !== this.props.value;
  }

  render() {
    return <p>Value: {this.props.value}</p>;
  }
}

				
			
  • getSnapshotBeforeUpdate(prevProps, prevState):Capture DOM information before it is updated.
				
					import React from 'react';

class MyComponent extends React.Component {
  getSnapshotBeforeUpdate(prevProps, prevState) {
    if (prevState.count < this.state.count) {
      return this.myRef.scrollHeight;
    }
    return null;
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    if (snapshot !== null) {
      this.myRef.scrollTop = snapshot;
    }
  }

  render() {
    return <div ref={ref => (this.myRef = ref)}>Content</div>;
  }
}

				
			
  • componentDidUpdate(prevProps, prevState, snapshot): Perform side-effects based on changes.
				
					import React from 'react';

class MyComponent extends React.Component {
  componentDidUpdate(prevProps, prevState) {
    if (prevProps.value !== this.props.value) {
      console.log('Value updated');
    }
  }

  render() {
    return <p>Value: {this.props.value}</p>;
  }
}

				
			

3. Unmounting

  • componentWillUnmount(): Clean up resources like timers or subscriptions.
				
					import React from 'react';

class MyComponent extends React.Component {
  componentDidMount() {
    this.timerID = setInterval(() => this.tick(), 1000);
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    console.log('Ticking');
  }

  render() {
    return <div>Timer running</div>;
  }
}

				
			

Lifecycle with Functional Components and Hooks

With functional components, lifecycle behavior is handled using hooks:
  • useState: Manages component state.
  • useEffect: Handles side-effects and replaces multiple lifecycle methods.
  • useRef: Accesses the DOM or stores mutable values.