Reactive UI Engine 45 min interactive guide🔄 Live Props & State Playground

React Props & State: Dynamic UIs & Data Flow

Master data management in modern React. Learn unidirectional props (primitives, arrays, objects, functions), immutability, creating and updating state with useState, triggering reactive re-renders, comparing Props vs State, and lifting state up.

01

1. What Are Props & Passing Props

Props (short for properties) are arguments passed into React components. Just like function arguments configure what a function does, props configure what a component displays.

// Parent passes props like HTML attributes
<UserProfile name="Alex" role="Architect" level={5} isVerified={true} />
03

3. Accessing Props (Destructuring)

In modern React, we always destructure props directly inside the function parameters:

export function UserProfile({ name, role, level = 1, isVerified }) {
  return (
    <div>
      <h2>{name} (Lvl {level})</h2>
      <p>{role}</p>
      {isVerified && <span>Verified</span>}
    </div>
  );
}
04

4. Props With Different Data Types

Data TypeJSX Syntax ExampleDescription
Stringtitle="Dashboard"Passed in standard quotes
Numbercount={42}Passed inside curly braces
BooleanisActive={true} or isActivePassed inside curly braces or shorthand
Arraytags={['React', 'Next.js']}Passed as JS array literal
Objectuser={{ id: 1, name: 'Aarav' }}Double curly braces (outer = JSX, inner = object)
FunctiononClick={() => alert('Clicked!')}Callback passed to handle child events
05

5. Props Are Read-Only & Top-Down Data Flow

React follows strict unidirectional (top-down) data flow. A child component must NEVER modify its own props.

🚫 Anti-pattern: props.count = props.count + 1; // ❌ ERROR: Props are immutable!
07

7. What Is State & useState Hook

Stateis a component's personal internal memory. Unlike local JavaScript variables that reset on every function run, state values are preserved across renders by React.

import { useState } from 'react';

export function Counter() {
  const [count, setCount] = useState(0);

  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}
09

9. Updating State & Re-rendering

When you invoke a state updater like setCount(1), React schedules a re-render. The component function runs again with the new state value, diffs the Virtual DOM, and updates only the changed elements on the screen.

💡 Functional Updater: If your new state depends on previous state, use: setCount(prev => prev + 1);
11

11. Props vs State (Side-by-Side Comparison)

FeaturePropsState
OriginPassed in from ParentCreated inside Component
MutabilityStrictly Read-Only (Immutable)Updated via setState
PurposeComponent configuration & communicationDynamic data that changes over time
Triggers Re-render?Yes, if parent passes new valuesYes, when setState is called
12

12. Lifting State Up & Passing Functions

When two sibling components need to communicate, you move the state to their common parent. The parent passes the state value to one child, and a callback function to the other child:

// Parent Component
export function App() {
  const [query, setQuery] = useState("");

  return (
    <>
      <SearchBar onSearch={setQuery} />
      <SearchResults query={query} />
    </>
  );
}
14

14. State With Objects and Arrays

Never mutate objects or arrays in state directly. Always use the spread operator to create a fresh copy:

// Updating an Object in State
setUser(prev => ({ ...prev, role: "Lead Architect" }));

// Adding to an Array in State
setItems(prev => [...prev, "New Item"]);
15

15. Common Props & State Mistakes

  • Direct state mutation: items.push("new") will NOT trigger a re-render. Always use setItems([...items, "new"]).
  • Calling setState synchronously in the component body: Causes infinite re-render loops. Put state setters inside event handlers or useEffect.
  • Expecting state to update immediately on the next line: State setters are asynchronous; state only updates on the next render cycle.
16

16. Building an Interactive Component

Combining props and state allows you to create rich, interactive widgets:

export function ToggleBadge({ label, defaultActive = false }) {
  const [isActive, setIsActive] = useState(defaultActive);

  return (
    <button
      className={isActive ? "badge-active" : "badge-inactive"}
      onClick={() => setIsActive(!isActive)}
    >
      {label}: {isActive ? "ON" : "OFF"}
    </button>
  );
}
17

17. Props & State Best Practices

  • Keep state as local as possible; only lift state up when siblings genuinely need it.
  • Derive values during render instead of creating redundant state (e.g. const fullName = firstName + ' ' + lastName;).
  • Always treat props as immutable contract parameters.
LIVE INTERACTIVE LAB

Props & State Interactive Playground

Experience how props flow down and state updates re-render UIs in real time across 3 interactive simulation modes!

Parent Component State (Props Provider)
isVipMember Prop:
Child Component: <UserProfileCard />
A
Alex Rivera
Staff Engineer
Discount: 20% OFF⭐ VIP Member
Props received: { name: "Alex Rivera", role: "Staff Engineer", discount: 20, isVip: true}
🎯 Props & State Challenge 1 of 5❌ Try Again

Goal 1: Change the parent's Name or Role prop in Tab 1 to see the child update live.

TEST YOUR KNOWLEDGE

React Props & State Mastery Quiz

8 scenario-based questions testing your understanding of Props vs State, useState mechanics, re-renders, functional updaters, and lifting state up.

Question 1 of 8Score: 0 / 8
⚖️ What is the primary difference between Props and State in React?