This document explains how data and state are passed between React components covering lifecycle phases, parent-child communication, and callback usage for effective state management.
This document explores how React components manage and exchange data and state, detailing the component lifecycle phases, parent-child and child-parent communication patterns, and practical examples for robust state management in modern React applications.
React components have three main lifecycle phases: mounting, updating, and unmounting. Each phase involves specific methods that control how components are created, updated, and removed from the DOM.
| Phase | Description |
|---|---|
| Mounting | Component is created and inserted into the DOM |
| Updating | Component re-renders due to state or prop changes |
| Unmounting | Component is removed from the DOM |
super(props).true by default).Data can be passed between React components in several ways, depending on their relationship.
The parent component passes data to the child via props. When the parent updates the prop values, the child receives the new data and re-renders.
1// Parent Component
2class App extends React.Component {
3 state = { name: 'John', color: 'green' }
4 render() {
5 return <AppInner name={this.state.name} color={this.state.color} />
6 }
7}
8
9// Child Component
10class AppInner extends React.Component {
11 render() {
12 return <div style={{ color: this.props.color }}>{this.props.name}</div>
13 }
14}
The parent passes a callback function to the child as a prop. The child calls this function to send data back to the parent.
1// Parent Component
2class App extends React.Component {
3 state = { time: '' }
4 setTime = currentTime => {
5 this.setState({ time: currentTime })
6 }
7 render() {
8 return <AppInner parentCallback={this.setTime} />
9 }
10}
11
12// Child Component
13class AppInner extends React.Component {
14 componentDidMount() {
15 setInterval(() => {
16 const now = new Date().toLocaleTimeString()
17 this.props.parentCallback(now)
18 }, 1000)
19 }
20 render() {
21 return <div>Sending time to parent...</div>
22 }
23}
| Relationship | Method | Example Mechanism |
|---|---|---|
| Parent to Child | Props | <Child prop=value /> |
| Child to Parent | Callback Prop | parentCallback(data) |
| Sibling to Sibling | State Manager | Redux, Context API |
Note
Redux and Context API are advanced topics for managing state between sibling components and are not covered in this module.
React components manage their own state and communicate through props and callbacks. Understanding the lifecycle phases and data flow patterns is essential for building robust, maintainable React applications.
(1) By using props to send values from parent to child
(3) getSnapshotBeforeUpdate is called before the DOM updates, not after.
| Method | Use Case |
|---|---|
| A. Props | 1. Parent to child communication |
| B. Callback Prop | 2. Child to parent communication |
| C. Redux | 3. Sibling to sibling sharing |
| D. State | 4. Local component data |
A-1, B-2, C-3, D-4.
Data can only be passed from parent to child in React, not from child to parent.
False. Data can be passed from child to parent using callback functions.
(4) Directly modifying another component’s state is not valid in React.