← Back to React Course | Chapter 3: Components & Props | Lesson 1 of 11

Function Components

A function component is a simple JavaScript recipe that takes ingredients (props) and returns a picture of what should appear on screen.

Writing a Basic Function Component

A function component is just a JavaScript function whose name starts with a capital letter and which returns JSX. React calls this function whenever it needs to know what to display. This is the simplest possible building block in React.

Note: Keep your first components small and focused on one piece of UI, like a single message or button.

Warning: A component name starting with a lowercase letter will be treated as a plain HTML tag, not your component.

Example: Writing a Basic Function Component

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
    function Welcome() {
      return <h1>Welcome to React!</h1>;
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<Welcome />);
  </script>
</body>
</html>

Returning JSX from a Function

The JSX returned by a function component describes what should appear on the page, but it is not real HTML -- it gets converted into function calls behind the scenes. Multi-line JSX needs to be wrapped in parentheses immediately after return so JavaScript doesn't insert an invisible semicolon.

Note: Wrap multi-line JSX in parentheses right after return to avoid accidental blank returns.

Warning: Putting return on its own line with the JSX starting on the next line, unwrapped, causes JavaScript to return undefined.

Example: Returning JSX from a Function

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
    function Profile() {
      return (
        <div>
          <h2>Jane Doe</h2>
          <p>Frontend Developer</p>
        </div>
      );
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<Profile />);
  </script>
</body>
</html>

Calling a Component Like an Element

Once defined, a function component is used in JSX just like a built-in HTML tag, but with a capital letter and angle brackets: <Welcome />. React sees this and calls your function to figure out what to render there, then does the same for every other component it finds.

Note: You can use the same component multiple times on a page just by writing its tag more than once.

Warning: Calling the function directly like Welcome() instead of <Welcome /> skips React's rendering process and can cause bugs.

Example: Calling a Component Like an Element

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
    function Greeting() {
      return <p>Hello there!</p>;
    }
    function App() {
      return (
        <div>
          <Greeting />
          <Greeting />
        </div>
      );
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<App />);
  </script>
</body>
</html>
Common Mistakes
  1. Forgetting to capitalize the component's name, causing React to treat it as an HTML tag instead of a component.
  2. Forgetting to return the JSX from the function, resulting in nothing being rendered.
  3. Adding a semicolon or other code between return and the opening parenthesis of multi-line JSX, breaking automatic semicolon insertion.
Chapter Summary
  • A function component is a plain JavaScript function that returns JSX.
  • It is the standard, modern way to write React components.
  • Function components can accept a single props argument.
  • They are simpler and more concise than class components for most use cases.
Browser Support

Function components with Hooks require React 16.8+; without Hooks they work in any React version.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.