ReactEasy
What are components?
Components are the building blocks of a React UI: JavaScript functions that accept inputs (props) and return JSX describing what should appear on screen. They can hold state, respond to events, and be nested and reused like custom HTML elements.
Key properties
- Names start with a capital letter (
<UserCard />), so React can tell them apart from HTML tags (<div>). - Pure rendering: given the same props and state, a component should return the same JSX, without side effects during render.
- Composition: components render other components, building a tree.
- Encapsulation: each component manages its own state and logic.
- Reuse: render the same component many times with different props.
Kinds you will hear about
- Function components: the standard today, using hooks for state and effects.
- Class components: legacy style with
render()and lifecycle methods; still supported. - Server Components: run only on the server (in frameworks like Next.js), can be
asyncand fetch data, and ship no JavaScript. - Client Components: interactive components, marked with
"use client"in Server Component frameworks.
Good practices
- Keep components small and focused on one job.
- Lift shared state to the closest common parent.
- Separate data fetching from presentation when it helps testing.
- Define components at the top level, never inside another component (that remounts them on every render).
Code example
tsx
1// A small component tree2type User = { id: number; name: string; role: string };34function Avatar({ name }: { name: string }) {5 return <span className="avatar">{name[0]}</span>;6}78function UserCard({ user }: { user: User }) {9 return (10 <article>11 <Avatar name={user.name} />12 <h3>{user.name}</h3>13 <p>{user.role}</p>14 </article>15 );16}1718export default function Team({ users }: { users: User[] }) {19 return (20 <section>21 {users.map((u) => (22 <UserCard key={u.id} user={u} />23 ))}24 </section>25 );26}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.