Introduction to React Components: Props and Reuse

A React component is a reusable piece of user interface, written as a JavaScript function that returns markup. Instead of writing the same card, button or heading markup again and again, you write it once as a component and then use that component wherever the same shape is needed. The component decides the structure, and the place that uses it supplies the details that change.
Props are how those details travel into a component. A prop is a value passed from a parent component to a child component, in a way that looks like an attribute in markup. The child reads the prop and uses it to render different text, an image or a link, while the structure stays the same. That split is the whole point: one structure, many uses, with the changing parts handed in as props.
Writing a first component
A component starts with a function whose name begins with a capital letter. That capital letter is how React tells a component apart from an ordinary HTML element. Inside the function, you return the markup the component should show. The markup looks like HTML, but it is written inside JavaScript, and it can include expressions inside curly braces.
The example below defines a greeting component. It takes a name as a prop and shows it inside a heading. Nothing else in the component changes with the name, which is what makes the component reusable. A page that needs the same greeting for three people uses the same component three times, with a different name each time.
function Greeting({ name }) {
return <h2>Hello, {name}</h2>;
}
function App() {
return (
<div>
<Greeting name="Sara" />
<Greeting name="Omar" />
<Greeting name="Lina" />
</div>
);
}
How props work in practice
Props can carry more than short strings. A prop can be a number, a list, a function or another piece of markup. A card component, for example, might receive a title string, a body string and a link address, and it renders all three in the same layout. A list component might receive a list of items and render one row for each item. The component does not need to know where the data came from, only what shape it has.
Props flow from parent to child, and a child component should treat the props it receives as read only. When a value needs to change in response to a click or an input, that change is handled with state, which lives in the component that owns the data. This one-way flow keeps the source of a value clear. If a value looks wrong on screen, you can trace it back to the parent that passed it, instead of hunting through every component that might have changed it.
function ArticleCard({ title, summary, url }) {
return (
<article>
<h3>{title}</h3>
<p>{summary}</p>
<a href={url}>Read more</a>
</article>
);
}
function ArticleList() {
return (
<ArticleCard
title="How flexbox works"
summary="A short guide to arranging items in a row or a column."
url="/guides/flexbox"
/>
);
}
The card above is a plain example of reuse. The same card component could render a second article by passing a different title, summary and link, and no copy of the card markup would be needed. In a real project, the data would often come from a list, and the parent would render one card per item. That pattern is the same idea at a larger scale: the parent owns the data, and the child renders one item at a time.
Composing small components
Composition means building a larger interface from smaller components, instead of writing one large component that contains everything. A page component might contain a header component, a list component and a footer component, and the list component might contain card components. Each level handles its own part, and each part can be changed without rewriting the rest.
| Pattern | When it helps | Watch out for |
|---|---|---|
| One component per repeated block | The same markup appears in more than one place | Props that grow until the component is hard to follow |
| Parent owns the data | Several children need the same value | Passing a prop through many layers that do not use it |
| Children as content | A wrapper needs flexible inner content | Wrapper logic that quietly depends on what is inside it |
Children are a special prop. When you place markup between the opening and closing tags of a component, React passes that markup in as the children prop. A layout component can use this to wrap different content in the same frame, without knowing in advance what that content will be. The guide to JavaScript DOM manipulation explains the plain JavaScript side of changing a page, which helps when you compare it with how React updates the screen for you.
Common mistakes
A frequent mistake is naming a component with a lowercase letter. React then treats it as an ordinary HTML element, and the component function never runs. The rendered output will be empty or wrong, and the error message may not point directly at the name. Checking the capital letter first saves a long search through the rest of the file.
A second mistake is trying to change a prop inside the child component. Props are read only from the child side, so assigning a new value to a prop does not update the parent, and it can lead to a screen that shows a stale value. When a child needs to ask the parent to change something, the parent passes a function as a prop, and the child calls that function. The parent then changes its own data, and the new value flows back down as a prop.
A third mistake is building one component that takes a long list of props to cover every possible variation. A card that needs a dozen props for every layout option is hard to use, and each new use adds another prop. In that case, splitting the card into smaller pieces, or using children for the variable part, usually gives a clearer result. Props should describe the data a component shows, not every styling choice it might ever need.
Where components do not help
Components do not remove the need to understand HTML, CSS and JavaScript. A component still renders HTML, it still needs CSS for layout and appearance, and its logic is still JavaScript. The CSS Flexbox and Grid guide is a good companion here, because layout inside a component follows the same rules as layout in a plain page. React changes how the markup is organised and updated, not the underlying pieces.
Components also do not fix a confused design on their own. If the same information is shown in three different shapes for no clear reason, wrapping each shape in a component keeps the confusion, it just moves it into separate files. Sorting out what each block should show, and which blocks are really the same block, comes before splitting the code. That thinking step is what makes reuse pay off, rather than adding files for their own sake.
Worked example: a greeting component with props
A component is a reusable piece of interface, written as a function that returns markup. A greeting component takes a name as a prop and returns a heading that uses it. The parent passes a different name each time it uses the component, and each use renders its own greeting. That reuse is the point: one definition, many uses, with the differences supplied as props.
function Greeting({ name }) {
return <h2>Hello, {name}</h2>;
}
function App() {
return (
<>
<Greeting name="Sam" />
<Greeting name="Priya" />
</>
);
}
Props flow in one direction, from parent to child. A child reads its props but should not change them, because the parent owns the data and other children may depend on it. When a child needs to change something, the parent passes down a function as a prop, and the child calls that function. That pattern keeps the data flow readable: data goes down, and requests to change it go up through functions the parent supplied.
Keep components small and named for what they show. A component called Greeting is easy to find and easy to reuse. A component called Section that renders a greeting, a list, a form and a footer is doing four jobs, and reusing any one of them means carrying the other three along. Splitting it into four components costs a little more code up front and saves far more when any one piece needs to change.
State: when a component needs to remember something
Props are inputs from a parent. State is data a component owns and changes itself, such as whether a panel is open or what a reader typed into a field. React provides the useState hook for that job. Calling it gives back the current value and a function to update it, and updating the value tells React to render the component again with the new value.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}
State updates are requests, not instant changes. Reading the state variable immediately after calling its setter still shows the old value, because the re-render has not happened yet. Code that needs the new value should compute it from the old value and use the computed result directly, rather than reading the state variable and hoping it has already changed. That habit prevents a whole class of off-by-one bugs in event handlers.
Place state as low as it can go. A counter that only one button needs should keep its state inside that button component, not in a distant parent. Lifting state up to a parent is the right move only when two or more components need the same value, such as a filter input and the list it filters. State kept close to its use is easier to follow and cheaper to re-render.
Lists, keys and conditional rendering
Rendering a list in React means mapping over an array and returning a component for each item. Each item needs a key, a stable identifier that helps React tell items apart when the list changes. A database id or a unique slug makes a good key. An array index makes a poor key for a list that can reorder or have items inserted, because the index of an item changes when the list changes, and React can then mix up which rendered output belongs to which item.
function TaskList({ tasks }) {
return (
<ul>
{tasks.map((task) => (
<li key={task.id}>{task.title}</li>
))}
</ul>
);
}
Conditional rendering shows or hides parts of a component based on state or props. A short-circuit expression works for showing something only when a condition is true, and a ternary expression works for choosing between two alternatives. Keep the conditions readable: a chain of nested ternaries inside markup is hard to follow, and moving the decision into a variable above the return statement usually reads better.
An empty list deserves its own treatment. Rendering nothing for an empty list can leave a reader wondering whether the list failed to load or is simply empty. A short message, such as "No tasks yet", answers that question directly, and it costs one condition. That small courtesy applies to loading states as well: a loading indicator tells the reader the data is on its way, while a blank area tells them nothing.
Common beginner mistakes in React
Mutating state directly is the most common React mistake. Changing an array with push or changing an object property in place does not tell React that anything changed, because the reference to the value is the same even though its contents differ. The fix is to create a new array or object with the change applied, using spread syntax or a mapping function, and pass that new value to the setter. React then sees a new reference and re-renders.
A second mistake is running side effects during rendering, such as fetching data or writing to storage inside the component body. Rendering should be a pure calculation from props and state to markup, because React may render a component more than once, and a side effect that runs during rendering can run more than expected. Effects belong in the useEffect hook, which runs after rendering and can be controlled with a dependency list.
A third mistake is forgetting that JSX is not HTML. Attributes use camelCase names, such as className instead of class, and self-closing tags need their closing slash. The compiler reports most of these as clear errors, so the fix is usually quick, but the habit of writing JSX as if it were HTML produces a steady stream of small errors until the differences become automatic.
Frequently asked questions
Can a component be used more than once on the same page?
Yes. A component is a reusable piece of user interface, and the same component can be rendered many times with different props. Each use keeps its own props, so one card component can show different titles and text in different places.
What happens if I forget to pass a prop?
The prop arrives as undefined inside the component, unless a default value is set. That often shows up as blank text or an error when the code tries to read a property from undefined. Checking for a missing prop early, or giving it a default, makes the problem easier to spot.
Do I need to learn JavaScript before React?
A working grasp of JavaScript helps a lot, because components are written with functions, variables and expressions. The JavaScript basics guide covers variables and functions, and the DOM guide explains how a page changes, both of which make React easier to follow.
What is the difference between props and state?
Props are inputs passed from a parent to a child, and the child should not change them. State is data a component owns itself, created with useState, and changed through its setter function. When two components need the same value, the state lives in their shared parent and is passed down as props.
Why does React warn about keys in a list?
Keys help React match each rendered item to the right data item when a list changes. Without a stable key, React may reuse the wrong output after an item is added, removed or reordered. Use a stable id from the data, not the array index, for lists that can change.


