Skip to main content

Describing the UI

React applications are built from isolated pieces of UI called components. A React component is a JavaScript function that returns markup.

Creating Your First Component

React components are regular JavaScript functions, but their names must start with a capital letter or they won’t work.

Understanding JSX

JSX is a syntax extension for JavaScript that lets you write HTML-like markup inside JavaScript files.

JSX Rules

Key differences from HTML:
  • Return a single root element (or use fragments <>...</>)
  • Close all tags (even self-closing like <img />)
  • Use camelCase for attributes (className not class, onClick not onclick)

Embedding JavaScript in JSX

Use curly braces {} to include JavaScript expressions:

Nesting Components

Components can render other components to create complex UIs:
Never define components inside other components! This causes performance issues and bugs. Always define each component at the top level.

Passing Props

Props let you pass data from parent to child components:

Default Props

Provide default values using JavaScript default parameters:

Organizing Component Files

As your app grows, organize components into separate files:

File Naming Conventions

  • Use PascalCase for component files: UserProfile.jsx, NavBar.jsx
  • Use lowercase for utility files: utils.js, helpers.js
  • Keep one component per file (with exceptions for small related components)

Complete Example

Here’s a complete example bringing everything together:

Next Steps

Now that you understand components and JSX, learn how to make your components interactive with event handlers.