Writing
React Components, State and Props Explained
A component is a function that returns UI, props are the read-only inputs its parent passes in, and state is its own memory that changes as the user acts. This refresh rewrites the examples as modern function components with hooks.
By Ka Lun Chan · Web development / Learning
What are components, state and props in React?
A component is a JavaScript function that returns UI. Props are the read-only inputs a parent passes into it, and state is the component’s own memory, which changes over time in response to what the user does. Write new components as functions and give them state with the useState hook.
I first wrote this on my old blog. This version keeps the explanation that held up, fixes the code samples, and updates what React has changed since.
The component is still the most important concept to learn in React. Once you can split a screen into components and decide which one owns each piece of changing data, props and state stop being confusing. The table below is the short version.
| Question | Props | State |
|---|---|---|
| What it is | Inputs passed in by the parent, like function arguments | The component’s own memory between renders |
| Who owns it | The parent that renders the component | The component that calls useState |
| Can the component change it? | No. Props are read-only. | Yes, through its set function |
| What changes it | The parent re-rendering with different props | User actions, network responses, timers |
| When the new value shows | On the next render with new props | On the next render after the set call |
| In the Counter example | count passed to CountDisplay | count held by CounterPanel |
Is React a library or a framework?
React is a library. Its own site describes it as the library for web and native user interfaces. React’s documentation also recommends starting a new app with a framework built on React, because React alone doesn’t give you routing, data fetching or a build setup.
When I first wrote this, I stressed the library point because people kept comparing React with full frameworks like Angular. That still matters: React handles rendering components and nothing else. What changed is the official advice on how to start. Creating a React App on react.dev recommends a framework such as Next.js or React Router, or Expo for native apps, and keeps building from scratch with a tool like Vite as an option for unusual constraints or for learning.
So React is a library you will usually use inside a framework. If you’re weighing that choice, I cover server rendering in React server-side rendering with Next.js, and pairing React with an existing backend in React on Rails.
Are function components still stateless?
No. That was true when I first wrote this, and it’s why I described function components as stateless and class components as stateful. Hooks changed it. A function component can hold state with useState and run side effects with useEffect, and react.dev now recommends functions for new code.
The original post said function components were easy to write but couldn’t use lifecycle methods, so anything interactive needed a class. With hooks, function components cover the same ground. React’s Component reference says class components are still supported, but React doesn’t recommend them in new code.
My samples in that post were also wrong. In the greeting, I put a template literal in single quotes, so it would have printed the placeholder text literally. My class version returned an object from render instead of JSX and read props without this. Here is the greeting written correctly as a function component. Note the capitalized name, which React requires:
// A component is a function that takes props and returns JSX
export default function Greeting({ name }) {
return <p>Hello, {name}</p>;
}
// Used by a parent:
// <Greeting name="KC" />If you maintain an older codebase full of classes, you don’t need to rewrite it all at once. Lifecycle methods still work, and I explain them in the React component lifecycle. Convert a class when you’re changing it anyway.
How does state work in a function component?
Call useState with a starting value. It returns the current value and a function to set the next one. Calling that function tells React to render the component again with the new value, and passing an updater such as c => c + 1 keeps the update correct when it depends on the previous value.
This is my original Counter rewritten as a modern function component:
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>{count}</p>
<button onClick={() => setCount(c => c + 1)}>
Click to increment
</button>
</div>
);
}The detail that trips people up is that state behaves like a snapshot. React’s guide to state as a snapshot explains that the value you read during a render doesn’t change while that render’s event handlers run. Setting state only changes it for the next render. React’s useState reference shows the same thing with a console.log right after the set call, which still prints the old value.
function handleTripleClick() {
// count is a snapshot for this render, so all three
// calls ask for the same value: count + 1
setCount(count + 1);
setCount(count + 1);
setCount(count + 1);
}
function handleTripleClickFixed() {
// Each updater receives the result of the previous one
setCount(c => c + 1);
setCount(c => c + 1);
setCount(c => c + 1);
}The first handler adds one, not three. The second adds three, because React queues each updater and feeds it the result of the one before. Updaters run during rendering, so keep them pure and only return the new value. My habit is to use the updater form whenever the next value is calculated from the current one.
For completeness, this is what the original class Counter should have looked like. It still works in current React:
import { Component } from 'react';
// Only for maintaining older code. New components should be functions.
export default class Counter extends Component {
state = { count: 0 };
handleClick = () => {
this.setState(prev => ({ count: prev.count + 1 }));
};
render() {
return (
<div>
<p>{this.state.count}</p>
<button onClick={this.handleClick}>Click to increment</button>
</div>
);
}
}How do props move data from a parent to a child?
The parent passes values as JSX attributes, and the child reads them as function arguments. A child can’t change its props. When a child needs to change something, the parent owns that state and passes down a function the child can call.
React’s page on passing props calls them immutable, and says a component that needs different props has to get them from its parent. That matches what I wrote originally: most components take data from props and render it, and only some need state.
Here the Counter is split into a parent that owns the count and two children that receive props. Moving shared state to the closest common parent is what react.dev calls lifting state up:
import { useState } from 'react';
function CountDisplay({ count }) {
return <p>Count: {count}</p>;
}
function IncrementButton({ onIncrement }) {
return <button onClick={onIncrement}>Click to increment</button>;
}
export default function CounterPanel() {
const [count, setCount] = useState(0);
return (
<div>
<CountDisplay count={count} />
<IncrementButton onIncrement={() => setCount(c => c + 1)} />
</div>
);
}CountDisplay and IncrementButton have no state at all. They are easy to test and reuse, and the one source of truth for count lives in CounterPanel.
Deciding where state goes is the real design work. Thinking in React gives a useful test. A value that never changes isn’t state. Neither is one that arrives from a parent as a prop. And if you can compute it from other state or props, it definitely isn’t state. Whatever survives those three questions is probably state, and it belongs in the closest component that every user of it sits under. When that parent ends up far above the components that use it, that is when a shared store starts to make sense, which I cover in React with Redux.
Why does React need keys when rendering a list?
Keys tell React which item is which between renders. Use a stable ID from your data, unique among siblings. Array indexes break when items are inserted, deleted or reordered, and random keys recreate every item on each render.
function TodoList({ todos }) {
return (
<ul>
{todos.map(todo => (
<li key={todo.id}>{todo.title}</li>
))}
</ul>
);
}React’s guide to rendering lists warns that index keys lead to subtle bugs once the order changes, because state and DOM end up attached to the wrong item. A list of components that each hold state, such as editable rows, is where this shows up first. If your data has no ID, create one when the item is created, for example with crypto.randomUUID(), never during render.
Sources
Checked in November against react.dev: the home page, Your First Component, Passing Props to a Component, Thinking in React, State as a Snapshot, Queueing a Series of State Updates, the useState and Component references, Sharing State Between Components, Rendering Lists and Creating a React App. The examples and the table are mine.
- React: home page (the library for web and native user interfaces)
- React Docs: Your First Component
- React Docs: Passing Props to a Component
- React Docs: Thinking in React
- React Docs: State as a Snapshot
- React Docs: Queueing a Series of State Updates
- React Reference: useState
- React Docs: Sharing State Between Components
- React Docs: Rendering Lists
- React Reference: Component (class components)
- React Docs: Creating a React App
Short answers
What is the difference between props and state in React?
Props are read-only inputs a parent passes to a component, like function arguments. State is the component’s own memory, created with useState, and it changes over time in response to user actions. A component can’t change its props, but it can update its state.
Are React function components stateless?
No. Hooks let function components hold state with useState and run side effects with useEffect. React’s documentation says class components are still supported but recommends function components for new code.
Why use setCount(c => c + 1) instead of setCount(count + 1)?
State is a snapshot for each render, so count doesn’t change until the next render. Several setCount(count + 1) calls in one handler all set the same value. The updater form receives the latest queued value, so each call builds on the one before.
Is React a library or a framework?
React is a library for building user interfaces. Its documentation recommends starting new apps with a framework built on React, such as Next.js or React Router, because React alone doesn’t provide routing, data fetching or a build setup.
Start with one component
Take a screen you’re building, draw a box around each component, and mark which values are props and which are state. If the same value shows up in two boxes, lift it to their common parent before you write any code.