Which of the following component names is valid in React?
React components must start with a capital letter and follow JavaScript identifier rules.
React components must start with an uppercase letter. 'MyComponent' is valid. 'myComponent' starts with lowercase, '123Component' starts with a digit, and '_myComponent' starts with an underscore which is allowed in JS but not recommended for React components.
What will React render when using a component named mybutton in JSX?
Consider these two components:
function mybutton() { return <button>Click</button>; }
function MyButton() { return <button>Click</button>; }And usage:
<mybutton />
React treats lowercase tags as HTML elements, not components.
React treats lowercase JSX tags as HTML elements. Since 'mybutton' is lowercase, React tries to render an HTML element <mybutton>, which is invalid HTML but React allows it. To render a component, the name must start with uppercase.
Given this React component code, why does it fail to render anything?
function 2ndComponent() {
return <div>Hello</div>;
}Check JavaScript rules for function names.
JavaScript function names cannot start with digits. '2ndComponent' is invalid syntax, so React cannot compile this component.
Why is it recommended to name React components using PascalCase (e.g., MyComponent)?
Think about how React distinguishes components from HTML elements.
React uses the first letter case to distinguish components from HTML tags. PascalCase signals React to treat the name as a component, while lowercase is treated as an HTML element.
Consider this React component:
function mycounter() {
const [count, setCount] = React.useState(0);
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}What will happen when you use <mycounter /> in your app?
Recall how React treats lowercase JSX tags and hooks usage.
React treats lowercase JSX tags as HTML elements, not components. Hooks like useState only work inside React components. Since 'mycounter' is lowercase, React renders an HTML element <mycounter> ignoring the function and hooks, so no state updates happen.