React Components

Build the UI from reusable functions.

✍️ 3 exercises + code challenge in this lesson

A component is a JavaScript function that returns JSX. Component names must start with a capital letter.

  • Use a component like an HTML tag: <Header />.
  • Components can contain other components, forming a tree.
  • In real projects, each component usually lives in its own file and is exported/imported.

Key Takeaways: React Components

Mastering React Components helps you write clean and reliable React code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

React Components Code Example

React
<div id="root"></div>
<script src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.25.6/babel.min.js"></script>

<script type="text/babel">
const { useState, useEffect, useRef, useMemo, createContext, useContext } = React;
function Header() {
  return <header style={{ background: "#071A2F", color: "white", padding: 12 }}>CodesInTech</header>;
}

function Card() {
  return <div style={{ border: "1px solid #CBD5E1", padding: 12, margin: "8px 0", borderRadius: 8 }}>A reusable card</div>;
}

function App() {
  return (
    <div>
      <Header />
      <Card />
      <Card />
      <Card />
    </div>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
Free sign-in required · Google login available

React Components Practice Exercises

Check what you learned in “React Components”.

/ 3 solved
Fill in the blank

Component names must start with…

function () {
  return <header>Logo</header>;
}
✓ Correct! Not quite. Try again.
Multiple choice

How do you use a component called Card?

✓ Correct! Not quite. Try again.
Code challenge

Create a Badge component that renders <span className="badge">New</span>, and render it twice inside App.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services