Sub topic 3
Splitting the UI into components with props
This chapter assumes you know JavaScript destructuring and the spread syntax, have a Vite React project running, and have written a function component with useState. You will split the to-do app into smaller components and pass data to them through props.
What props are
Props (short for properties) are the inputs to a component. React passes them as a single object, and you usually destructure that object in the parameter list. Props flow one way: from a parent to its children. A child must not change its props. If it needs to change something, it asks the parent through a callback.
Extracting TodoItem
In the earlier chapters, App rendered each title directly inside an <li>. A to-do needs a checkbox and a delete button, so it deserves its own component. TodoItem is presentational: it shows what it receives and reports user actions. It owns no state.
function TodoItem({ todo, onToggle, onDelete }) {
return (
<li>
<label>
<input
type="checkbox"
checked={todo.done}
onChange={() => onToggle(todo.id)}
/>
<span style={{ textDecoration: todo.done ? "line-through" : "none" }}>
{todo.title}
</span>
</label>
<button type="button" onClick={() => onDelete(todo.id)}>
Delete
</button>
</li>
);
}The component takes three props:
| Prop | Type | Purpose |
|---|---|---|
| `todo` | object | The data to display: `id`, `title`, `done` |
| `onToggle` | function | Called with the id when the checkbox changes |
| `onDelete` | function | Called with the id when Delete is clicked |
The on prefix is a common convention for callback props. It marks them as things that happen, not things to display.
Using it from the parent
App keeps the state and passes pieces down. Inside the <ul>, replace the plain <li> with the new component:
<ul>
{todos.map((todo) => (
<TodoItem
key={todo.id}
todo={todo}
onToggle={toggleTodo}
onDelete={deleteTodo}
/>
))}
</ul>toggleTodo and deleteTodo are functions defined in App. You will write them in a later chapter. For now, you only need to see that the child receives them as props. The key stays on the outermost element returned by map, which is now TodoItem. React reads key itself, so the child never receives it as a prop.
Common errors
- Passing a call instead of a function. Writing
onClick={onDelete(todo.id)}runsonDeleteduring render. Wrap it:onClick={() => onDelete(todo.id)}. - Mutating a prop. Writing
todo.done = trueinside the child changes the parent's data without triggering a re-render. CallonToggleinstead. - Putting the key inside the child. A
keyon the<li>insideTodoItemdoes nothing useful. The key belongs where the array is mapped. - Defining components inside components. A function declared inside
Appis a new component on every render, so React discards its DOM and any state each time. Declare components at module level.
Composition
Components can also receive other elements through the special children prop. A wrapper such as <Card>…</Card> renders whatever is placed between its tags. You will not need it for the to-do list, but it is the main way to build layout components.
Exercise
In <TodoItem todo={todo} onToggle={toggleTodo} onDelete={deleteTodo} />, which props carry data down and which let the child notify the parent? Why must the child not set todo.done = true itself?
Show answer
todo carries data down. onToggle and onDelete are callback props that let the child report user actions to the parent. Setting todo.done = true mutates the parent's state object directly. React does not re-render because no state setter was called, and the change breaks the one-way data flow. The child should call onToggle(todo.id), and the parent should create a new array with setTodos.