Naming rules
- React course
- Beginner
- Lesson 11/84
- 4 min
Learn that component names start with a capital letter and why it matters.
Naming rules
In React, component names use PascalCase: UserCard, ShoppingCart, LoginForm. This separates them from HTML tags.
HTML tags start lowercase: div, span, button. If a component name starts lowercase, React treats it as an HTML tag and it won't work.
Component names should also describe what they do: UserList is clear, Comp1 is vague. Good names make maintenance easier.
Naming meaning
A component name should describe what it does. Header is a clear top bar; Thing says nothing. Teams often follow the what + for what rule: UserAvatar, CartItem, SearchInput.
The file name should match the component name: a UserAvatar.jsx file holds the UserAvatar component. This makes searching easy.
Consistent naming speeds up team talk: when someone hears a name, everyone pictures the same thing.
Practice UserAvatar component
Complete the skeleton. Write a component called UserAvatar that returns <figure> with <img alt="Avatar" /> inside. Don't forget to export.
Starter code
// Write the UserAvatar component hereSample solution
function UserAvatar() {
return <figure><img alt="Avatar" /></figure>;
}
export default UserAvatar;Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is a valid component name?
- userCard
- user_card
- UserCard Correct answer
- user-card
Explanation: UserCard is PascalCase, so it's a valid component name.
2 PascalCase example
Valid component name: ____ (PascalCase).
Valid component name: ProductCard (PascalCase).
Explanation: ProductCard is PascalCase.
3 Match the naming tips
| Term | Meaning |
|---|---|
UserCard |
Meaningful name |
userCard |
Wrong |
UserCard.jsx |
File name |
User-Card |
Invalid |