Good component names help you and others understand what each part does. It makes your code easier to read and fix later.
Component naming rules in React
Introduction
Syntax
React
function ComponentName() { return <div>Hello!</div>; }
Component names must start with a capital letter.
Use PascalCase (each word starts with a capital letter) for component names.
Examples
React
function UserProfile() { return <div>User Profile</div>; }
React
function userprofile() { return <div>User Profile</div>; }
React
const Header = () => { return <header>Site Header</header>; };
Sample Program
This example shows a component named WelcomeMessage starting with a capital letter. It is used inside the App component.
React
import React from 'react'; function WelcomeMessage() { return <h1>Welcome to React!</h1>; } export default function App() { return ( <main> <WelcomeMessage /> </main> ); }
Important Notes
Always start component names with a capital letter so React knows it is a component.
Use clear and descriptive names to make your code easier to understand.
Do not use special characters or spaces in component names.
Summary
Component names must start with a capital letter.
Use PascalCase for naming components.
Clear names help keep your code readable and maintainable.
Practice
1. Which of the following is the correct way to name a React component?
easy
Solution
Step 1: Understand React component naming rules
React components must start with a capital letter to be recognized as components.Step 2: Identify the correct naming style
PascalCase (capitalizing each word) is the standard for React components, so "MyComponent" is correct.Final Answer:
MyComponent -> Option AQuick Check:
Component names start with capital letters = C [OK]
Hint: Component names always start with uppercase letters [OK]
Common Mistakes:
- Starting component names with lowercase letters
- Using underscores instead of PascalCase
- Using all lowercase letters
2. Which of the following will NOT be recognized by React as a component?
easy
Solution
Step 1: Check function component naming
React treats functions starting with lowercase letters as regular functions, not components.Step 2: Identify the naming error
While lowercase function names are valid JavaScript, React will not render them as components, causing unexpected behavior.Final Answer:
function mycomponent() { return <div>Hello</div>; } -> Option AQuick Check:
Component functions must start uppercase = D [OK]
Hint: Component functions must start with uppercase letters [OK]
Common Mistakes:
- Using lowercase function names for components
- Confusing JavaScript function syntax with React rules
- Assuming lowercase names work as components
3. What will happen if you try to render this component?
function greeting() { return <h1>Hi</h1>; }<greeting />medium
Solution
Step 1: Understand lowercase component names in JSX
React treats lowercase tags as HTML elements, not components.Step 2: Analyze the effect of rendering <greeting />
Since "greeting" is lowercase, React looks for an HTML tag <greeting>, which does not exist, so it renders nothing.Final Answer:
It renders nothing because React treats it as a lowercase HTML tag -> Option DQuick Check:
Lowercase names render as HTML tags = A [OK]
Hint: Lowercase names render as HTML tags, not components [OK]
Common Mistakes:
- Expecting lowercase names to render components
- Thinking React throws syntax errors for lowercase names
- Assuming React warns automatically for lowercase components
4. Identify the error in this React component code:
function 1stComponent() { return <div>Hello</div>; }medium
Solution
Step 1: Check component naming rules
Component names must be valid JavaScript identifiers and start with a capital letter.Step 2: Identify invalid name
Names cannot start with a number, so "1stComponent" is invalid.Final Answer:
Component name cannot start with a number -> Option BQuick Check:
Valid identifiers start with letters = B [OK]
Hint: Component names must start with letters, not numbers [OK]
Common Mistakes:
- Starting component names with numbers
- Confusing lowercase requirement
- Assuming JSX syntax error when name is invalid
5. You want to create a component that shows a user's profile. Which is the best name following React naming rules and good practice?
hard
Solution
Step 1: Apply React component naming conventions
React components should use PascalCase starting with uppercase letters.Step 2: Choose clear and readable name
"UserProfile" uses PascalCase and clearly describes the component's purpose.Final Answer:
UserProfile -> Option CQuick Check:
PascalCase and clarity = A [OK]
Hint: Use PascalCase with clear words for component names [OK]
Common Mistakes:
- Using all lowercase or underscores in names
- Mixing lowercase and uppercase incorrectly
- Choosing unclear or ambiguous names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
