Browse Courses

React States

This document explains the concept of state in React class components, how it differs from props, and how state enables dynamic, interactive user interfaces.

This document explores state in React class components, including local and shared state, how state enables dynamic UI updates, and the differences between state and props. It provides practical examples and a comparison table for clear understanding.


Introduction to State in React

State in React is a plain JavaScript object used to represent information about a component’s current situation. State allows components to create dynamic and interactive user interfaces by tracking and responding to changes in data.


Types of State: Local and Shared

  • Local State: Lives within a single component and cannot be accessed by others. Used for UI elements like toggles or form fields.
  • Shared State: Can be accessed and modified by multiple components, often managed in a parent component and passed down via props.

How State Works in Class Components

  • State is initialized in the constructor as an object with key-value pairs.
  • State is updated using this.setState, which triggers a re-render of the component.
  • State is used to track changes in forms, buttons, timers, and more.

Example: Counter with State

 1class Counter extends React.Component {
 2  constructor(props) {
 3    super(props)
 4    this.state = { count: 0 }
 5  }
 6
 7  increment = () => {
 8    this.setState({ count: this.state.count + 1 })
 9  }
10
11  render() {
12    return (
13      <div>
14        <p>Count: {this.state.count}</p>
15        <button onClick={this.increment}>Increment</button>
16      </div>
17    )
18  }
19}

Props vs State

Props and state are both used to manage data in React, but they serve different purposes:

FeatureStateProps
MutabilityMutable (can be changed by the component)Immutable (read-only in child)
ScopeLocal to the componentPassed from parent to child
UsageManages internal data and UI updatesPasses data and methods to children
ModificationUpdated with setStateCannot be modified by the child

Example: Using Props and State Together

 1class TestComponent extends React.Component {
 2  constructor(props) {
 3    super(props);
 4    this.state = { id: 1, name: props.name, age: 25 };
 5  }
 6
 7  render() {
 8    return (
 9      <div>
10        <p>Name: {this.state.name}</p>
11        <p>Age: {this.state.age}</p>
12      </div>
13    );
14  }
15}
16
17// Usage
18<TestComponent name="John" />
19<TestComponent name="Jill" />

Key Points

  • State enables components to track and respond to user interactions.
  • State changes cause components to re-render, keeping the UI up to date.
  • Props are used to pass data and methods from parent to child in a unidirectional flow.
  • State is managed within the component, while props are received from outside.

Conclusion

State is fundamental to building interactive React components. It allows components to manage their own data, respond to user actions, and update the UI dynamically. Understanding the distinction between state and props is essential for effective React development.


FAQ

One-way binding in React means data flows from parent to child via props, and children cannot directly modify parent data. This makes data flow predictable and easier to debug.

Directly mutating a state object does not trigger a re-render, so the UI will not update. Always use the state setter function to update state.

  1. State allows components to manage and track dynamic data that can change over time.
  2. State is used to pass data from parent to child components.
  3. State is only for styling components.
  4. State is used to define static content.
(1) State allows components to manage and track dynamic data that can change over time, enabling interactive UIs.

The component will re-render, updating the user interface to reflect the new state values.

  1. Props are immutable in the child component.
  2. Props are used to pass data from parent to child.
  3. Props can be modified by the child component.
  4. Props enable component reuse.
(3) Props cannot be modified by the child component; they are read-only.

Local state is confined to a single component, while shared state is managed in a parent and accessed by multiple components via props.

State is managed within a component and can be changed, while props are received from outside and are immutable in the child.

ConceptDescription
A. State1. Passes data from parent to child
B. Props2. Manages internal data and UI updates
C. setState3. Updates the component’s state and triggers a re-render
A-2, B-1, C-3.

Using setState ensures that React knows about the state change and can re-render the component. Direct modification does not trigger a re-render.

Props can be used to initialize state or pass data and methods from parent to child, while state manages the component’s own dynamic data.

Ensure that setState is being used correctly and that the state is not being modified directly.

State changes in a React component always trigger a re-render of that component.

True. Updating state with setState causes the component to re-render and update the UI.