Course topics

By WebNest Studio Engineering Team

React Tutorial

Props in React

Props (short for "properties") are how data flows from a parent component into a child component. They work like function arguments: a parent passes values through JSX attributes, and the child receives all of them bundled into a single object, conventionally named props.

Props always flow in one direction, from parent to child — this is called "one-way data flow" or "unidirectional data flow." A child component can read the props it receives, but it can never modify them; props are read-only from the child's perspective. If a component needs to change data over time, that data belongs in state, not in a prop the component itself owns.

Why it matters

Props are how data flows from a parent component down to a child. They make one component reusable with different content, and their one-way, read-only nature is what makes a React application predictable. Almost every bug report about "the child is not updating" comes down to how props are passed.

Passing and Reading Props

A parent passes props exactly like HTML attributes: <Greeting name="Asha" age={28} />. The child receives a single props object, { name: "Asha", age: 28 }, which it can access as props.name or, more commonly, destructure directly in the function signature as function Greeting({ name, age }).

Props Are Read-Only

React enforces that components must never mutate their own props. This "purity" rule keeps data flow predictable: given the same props, a component should always render the same output. If you find yourself wanting to reassign a prop inside a component, that is a signal the value should instead be copied into local state, or the change should be lifted up and handled by the parent that owns the data.

Default Values, children, and Prop Types

Function components can specify default values for props using default parameters, for example function Button({ label = "Click me" }). The special prop children contains whatever JSX is nested between a component's opening and closing tags, which is how wrapper components like layout containers or modals render arbitrary content passed to them. In JavaScript projects, tools like PropTypes or TypeScript interfaces are commonly used to document and validate the shape of expected props.

Example

Passing props down, using defaults, and rendering children

JavaScript
function Card({ title, children }) {
  return (
    <section className="card">
      <h2>{title}</h2>
      <div className="card-body">{children}</div>
    </section>
  );
}

function PriceTag({ amount, currency = 'USD' }) {
  return <span>{amount.toFixed(2)} {currency}</span>;
}

export default function ProductPage() {
  return (
    <Card title="Wireless Headphones">
      <p>Noise-cancelling, 30-hour battery life.</p>
      <PriceTag amount={79.5} />
    </Card>
  );
}
Output
Renders a card section titled "Wireless Headphones" containing the description paragraph and a price tag reading "79.50 USD" — the currency defaulted to USD because PriceTag was not given one explicitly.

Common Mistakes

  • Attempting to reassign a prop value inside a child component (e.g. "props.name = 'new value'") instead of treating props as read-only input.
  • Forgetting that a prop not passed by the parent is undefined unless a default value is defined, then calling a method on it and crashing.
  • Passing an entire object as one prop and then destructuring it awkwardly inside the child, instead of passing only the specific fields the child actually needs.
  • Confusing props (data passed in from a parent) with state (data a component manages internally) and trying to update a prop directly to trigger a re-render.

Exercise

Do this in src/App.jsx of a Vite + React project. Write a Card component that takes a title prop, a tone prop that defaults to neutral, and children. It renders a section whose class is the tone, with the title in an h2 and the children beneath it. Use it twice in App.

Expected result: two cards, the first with the class neutral and the second with the class warning, each showing its own title and content.

JavaScript · starter code
// TODO: function Card({ title, tone = "neutral", children })

export default function App() {
  return (
    <>
      {/* TODO: a Card titled "Welcome" containing a paragraph */}
      {/* TODO: a Card titled "Heads up" with tone "warning" containing a paragraph */}
    </>
  );
}
Show hints
  • Props are read by destructuring the function parameter, and a default is written there: { title, tone = "neutral", children }.
  • Whatever is placed between the opening and closing tags of a component arrives as the children prop.
Show solution
JavaScript · solution
function Card({ title, tone = "neutral", children }) {
  return (
    <section className={tone}>
      <h2>{title}</h2>
      {children}
    </section>
  );
}

export default function App() {
  return (
    <>
      <Card title="Welcome">
        <p>Pick a course to begin.</p>
      </Card>
      <Card title="Heads up" tone="warning">
        <p>Your session expires soon.</p>
      </Card>
    </>
  );
}

Quick quiz

1. Can a component change the props it receives?

  • A. Yes, by assigning to them
  • B. Only inside useEffect
  • C. Only with setProps
  • D. No; props are read-only
Show answer

D. Data that changes belongs in state, owned by the component that changes it.

2. In which direction do props flow?

  • A. From parent to child
  • B. From child to parent
  • C. Between siblings
  • D. In both directions
Show answer

A. A child communicates upwards by calling a function that the parent passed down as a prop.

3. What is the children prop?

  • A. A list of child components' names
  • B. The content placed between a component's opening and closing tags
  • C. The number of child elements
  • D. A reserved state variable
Show answer

B. It lets a component wrap arbitrary content, as a card or a layout does.

Interview questions

What is the difference between props and state?

Show answer

Props are passed into a component by its parent and are read-only inside it; they are the component's inputs. State is data the component owns and can change with a setter function, and changing it causes a re-render. A value that the component must modify over time is state; a value it merely receives and displays is a prop. State in a parent often becomes a prop of a child.

What is prop drilling and how is it avoided?

Show answer

Prop drilling is passing a prop through several layers of components that do not use it themselves, only so that a deeply nested component can receive it. It makes the intermediate components harder to change and reuse. It is avoided with composition, by passing the nested component itself as children; with Context for data needed widely, such as the current user or theme; or with a state management library.

Key Points to Remember

  • Props pass data one-way, from a parent component down to a child component, via JSX attributes.
  • Props are read-only — a component must never mutate the props object it receives.
  • Default parameter values give a prop a fallback when the parent does not supply one.
  • The special children prop carries whatever JSX is nested inside a component's tags.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of plain HTML, CSS and JavaScript. JSX needs a React project on your computer. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering