React Reference ยท JSX Syntax and Rules
Member tags and casing in JSX
How JSX decides between an HTML tag and a component: lowercase names are strings, capitalized names and dotted member tags are variables. With dynamic tags.
The first thing the JSX compiler checks in a tag is its name. A name that starts with a lowercase letter and has no dots, such as div or section, becomes the string "div": a built-in HTML (or SVG) element. A name that starts with an uppercase letter, such as Card, is read as a JavaScript variable in scope, which should hold a component.
A name with a dot, such as Menu.Item or icons.Star, is a member expression and is always treated as a variable, even when the last part is lowercase. That makes it possible to group components on one object and to choose a component at runtime.
<div /> // string "div" <Card /> // variable Card <Menu.Item /> // variable Menu, property Item <icons.star /> // variable icons, property star (still a component reference)
Try it
Syntax
| Tag name | Compiles to the type | Meaning |
|---|---|---|
<div> |
"div" |
Built-in element |
<Card> |
Card |
Variable in scope |
<Menu.Item> |
Menu.Item |
Property of a variable |
<a.b.c> |
a.b.c |
Deeper member chain |
<this.Part> |
this.Part |
Member of this (class components) |
<my-element> |
"my-element" |
Custom element (a name with a dash) |
Not valid: <card> for a component you defined as Card (it renders an unknown HTML element named card, and React warns). <props.type> works only when props.type holds a component or string; a bare <type> does not read a variable.
Usage notes
- Casing is semantic. Name every component with an initial capital. A lowercase local variable cannot be used directly as a tag; assign it to a capitalized one first (
const Tag = tag;). - A string is a valid type.
const Tag = 'h2'; <Tag />renders an<h2>, which is how "as" props and heading levels are built. - Do not create components during render.
const Inner = () => ...declared inside another component gets a new identity each render, so React remounts it and loses its state. Declare components at module level. - Namespacing keeps exports tidy. Compound components such as
Tabs.ListandTabs.Panelcommunicate that they belong together; see compound components. - Conditional types are fine.
const Wrapper = fancy ? FancyBox : 'div'picks the type for one render; keep the choice stable between renders when it holds state. - Dotted names cannot contain computed access.
<items[0] />is a syntax error; copy it to a variable first.