Understanding React Components: Props, State, and Rendering Basics

Someone asked kirupaBot this question over on kirupa.com:

How do React components work?

kirupaBot’s answer

Great question. Let’s ground this in Kirupa-like intuition, then show how components fit together with a practical, example-driven view.

Intuition: what a React component really is

  • A component is a self-contained UI piece. It describes what you want to see (the UI) and how it should respond to changes.
  • Each component has props (inputs from its parent) and state (internal data it can change over time).
  • React renders the component tree. When props or state change, React re-renders the affected parts of the tree to keep the UI in sync.
  • Components can be nested. Parents pass data down via props; children can signal intent (like “do this”) back up via callbacks.

How state and props work together

  • Props flow downward: a parent renders a child with attributes, e.g., <Child someProp={value} onAction={handleAction} />.
  • State flows upward via callbacks: a child can call onAction(...), and the parent can update its state, which then passes new props back down.
  • This unidirectional data flow makes reasoning about UI changes predictable.

Common patterns you’ll see in Kirupa material

  • Lifting state up: if two components need to reflect the same value, move that state to their closest common parent and pass it down as props. The parent owns the state and provides setters to children as callbacks [3].
  • Using useState in function components: declare state with an initial value, get back [state, setState], and update with setState(...) (or the functional form when you rely on the previous value) [General useState explanation in prior convo; see Source 3 and Source 4].
  • When state gets more complex or shared across many parts of the UI, you might lift state up, split it into multiple contexts, or use useReducer for centralized update logic (as discussed in “Dealing with State in React” and related excerpts) [Source 1, Source 3, Source 4].

A minimal, practical example: a simple component with state and props

  • A counter that increments locally, with a parent controlling the Reset action.
import React, { useState } from 'react';

function Counter({ step = 1, onReset }) {
  const [count, setCount] = useState(0);

  const increment = () => setCount(prev => prev + step);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
      <button onClick={onReset}>Reset (from parent)</button>
    </div>
  );
}

// Example usage in a parent
function App() {
  const [resetKey, setResetKey] = useState(0);

  const handleReset = () => {
    // this could trigger something in the child, or manage state in parent
    setResetKey(k => k + 1);
  };

  return (
    <div>
      <Counter step={2} onReset={handleReset} key={resetKey} />
    </div>
  );
}

Notes:

  • The Counter component owns its own internal state (count). It also receives a prop onReset to signal the parent when a reset is requested.
  • The parent can respond to that signal by updating its own state and, if needed, changing what the child displays via props (lifting state up or re-rendering with new props).

Key takeaways

  • React components are the building blocks: they encapsulate UI, state, and logic, and compose via props and callbacks.
  • State is local to a component unless you lift it up or put it in a shared store/context.
  • For growing apps, consider useReducer for complex update logic, and context or external state libraries when many components need to react to shared data without excessive re-renders [Source 3, Source 4, Source 1].

If you want, tell me a scenario you’re curious about (like a header reacting to cart changes, or a form with validation), and I’ll walk through how to model it with components, props, state, and maybe useReducer or context as appropriate.

Sources from kirupa.com


This topic continues a kirupaBot chat so real humans can weigh in. Did kirupaBot get it right? What would you add or explain differently? Reply below!