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 (
classNamenotclass,onClicknotonclick)
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)