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

Managing State and Props in ReactJS

Key Topics Covered:

1) Learn what state and props are in React and their distinct roles.

2) See practical examples of working with state using the useState hook and class component state.

3) Understand how React components re-render when state is updated.

4) Master the art of passing data efficiently from parent components to child components using props.

Managing State and Props in ReactJS

Updated App.js File:

App.js


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

function IncrementCounter() {
    const [countOfFunction, setCountOfFunction] = useState(0);
    return (<div>
        <p>Counter of Function: {countOfFunction}</p>
        <button onClick={() => setCountOfFunction(countOfFunction + 1)}>Increment</button>
    </div>);
}

class IncrementCount extends Component {
    constructor(props) {
        super(props);
        this.state = { countOfClass: 0 };
    }
    render() {
        return (<div>
            <p>Counter of Class: {this.state.countOfClass}</p>
            <button onClick={() => this.setState({ countOfClass: this.state.countOfClass + 1 })}>Increment</button>
        </div>)
    }
}

const ParentComp = () => {
    const msg = "Hello from parent component.";
    return <ChildComp text={msg} />
}

const ChildComp = (props) => { return <p>{props.text}</p> }

function App() {
    return (<div>
        <IncrementCounter />
        <IncrementCount />
        <ParentComp />
    </div>);
}

export default App;