How to Create Components in React
What you’ll build or solve
You’ll create a reusable React component and render it in your app.
When this approach works best
This approach works best when:
Learn React on Mimo
- You want to split a large UI into smaller pieces.
- You reuse the same layout or logic in multiple places.
- You want cleaner, more maintainable code.
- You build a feature that deserves its own self-contained block.
This is a bad idea if you create tiny components for single elements with no reuse. Keep components meaningful.
Prerequisites
- A working React project
- Basic understanding of JSX
- Familiarity with JavaScript functions
Step-by-step instructions
Step 1: Create a function component
React components are plain JavaScript functions that return JSX.
Option A: Function declaration
JSX
function Welcome() {
return <h1>Hello, React</h1>;
}
Option B: Arrow function
JSX
const Welcome = () => {
return <h1>Hello, React</h1>;
};
Both approaches create a valid React component.
To use the component, render it inside another component:
JSX
function App() {
return (
<div>
<Welcome />
</div>
);
}
What to look for
- Component names must start with a capital letter.
- The function must return JSX.
- JSX must return a single root element.
Use a fragment if needed:
JSX
function Example() {
return (
<>
<h1>Title</h1>
<p>Description</p>
</>
);
}
Lowercase names are treated as built-in HTML tags, not custom components.
You can pass data using props:
JSX
function Welcome({ name }) {
return <h1>Hello, {name}</h1>;
}
<Welcome name="Alex" />;
For larger projects, move the component into its own file and export it:
JSX
export default Welcome;
Add state later with hooks like useState if your component needs to react to user input.
That is the core creation process.
Examples you can copy
Example 1: Simple display component
JSX
function Title() {
return <h1>Dashboard</h1>;
}
Use it:
JSX
<Title />
Example 2: Component that receives props
JSX
function Greeting({ name }) {
return <p>Welcome, {name}</p>;
}
Use it:
JSX
<Greeting name="Sam" />
<Greeting name="Riley" />
Each instance renders with its own data.
Example 3: Component in a separate file
src/components/Button.jsx:
JSX
const Button = ({ label }) => {
return <button>{label}</button>;
};
export default Button;
Import and use:
JSX
import Button from "./components/Button";
function App() {
return <Button label="Click me" />;
}
Common mistakes and how to fix them
Mistake 1: Using a lowercase component name
What you might do:
JSX
function welcome() {
return <h1>Hello</h1>;
}
Why it breaks:
React treats lowercase tags as HTML elements.
Correct approach:
JSX
function Welcome() {
return <h1>Hello</h1>;
}
Mistake 2: Forgetting to return JSX
What you might do:
JSX
const Card = () => {
<div>Content</div>;
};
Why it breaks:
The function does not return anything.
Correct approach:
JSX
const Card = () => {
return <div>Content</div>;
};
Or use implicit return:
JSX
const Card = () => <div>Content</div>;
Mistake 3: Returning multiple elements without a wrapper
What you might do:
JSX
function Example() {
return (
<h1>Title</h1>
<p>Text</p>
);
}
Why it breaks:
JSX must return one root element.
Correct approach:
JSX
function Example() {
return (
<>
<h1>Title</h1>
<p>Text</p>
</>
);
}
Troubleshooting
If your component does not render, confirm the name starts with a capital letter.
If you see “is not defined,” check your import path and export statement.
If you get an error about adjacent JSX elements, wrap them in a fragment.
If nothing appears on screen, confirm the component is actually used inside App.
Quick recap
- A React component is a JavaScript function that returns JSX.
- Use a capitalized name.
- Return a single root element or fragment.
- Render it like
<ComponentName />. - Export and import components when organizing files.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot