Loading . . .

Introduction to Mastering ReactJS

Getting Started with ReactJS

Understanding Components in ReactJS

Handling Events in ReactJS

Managing State and Props in ReactJS

Component Lifecycle and Hooks in ReactJS

JSX & Conditional Rendering in ReactJS

Styling React Components

Fragments & Lists in ReactJS


Disclaimer: Educational Purposes Only

The content on this website, including videos and code examples, is for educational purposes only. All demonstrations and designs are fictional and created to illustrate coding techniques. Any resemblance to existing websites or brands is purely coincidental.

The creators and administrators of this website do not claim ownership or affiliation with any existing websites or companies. Users are encouraged to use the information responsibly for learning purposes. Liability for any misuse of the content provided is not accepted.

Download Notes

Component Lifecycle and Hooks in ReactJS

Key Topics Covered:

1) Understanding the Lifecycle of a React Component.

2) Class Component Lifecycle Methods.

3) Introduction to React Hooks.

4) Migrating Class Components to Functional Components with Hooks.

Component Lifecycle and Hooks in ReactJS

Updated App.js File:

App.js


import React, { Component, useEffect, useState } from 'react'

class MyComp extends Component {
    componentDidMount() {
        // fetchData().then(data => this.setState({ data }))
    }

    componentDidUpdate(prevprops, prevstate) {
        // check if props or state has changed
        // do something
    }

    componentWillUnmount() {
        // cleanup operations
    }

    render() {
        return (<div>{/* render Component */}</div>);
    }
}

function MyComp2() {
      const [stateVal, setStateVal] = useState(0)

      useEffect(() => {
          // fetchData().then(response => setStateVal(response))
      }, [])

      return (<div>{stateVal}</div>);
}

function ConvertClassToFunction() {
    const [stateHere, setStateHere] = useState(0)

    useEffect(() => {
        // something on mount

        return () => {
            // cleanup on unmount
        }
    }, [])

    useEffect(() => {
        // something for update
    }, [stateHere])

    return (<div>{stateHere}</div>);
}

function App() {
    return (<div>Mastering ReactJS</div>);
}

export default App;