180

I am playing around with the ReactJS framework on JSBin.

I have noticed that if my component name starts with a lowercase letter it does not work.

For instance the following does not render:

var fml = React.createClass({
  render: function () {
    return <a href='google.com'>Go</a>
  }
});

React.render(<fml />, document.body);

But as soon as I replace the fml with Fml it does render.

Is there a reason I cannot begin tags with small letters?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
shaunakde
  • 3,009
  • 3
  • 24
  • 39
  • check the answers of this question for some more detials: [Html is not rendering in the browser - React js](https://stackoverflow.com/questions/42110144/html-is-not-rendering-in-the-browser-react-js) – Mayank Shukla Jul 03 '17 at 07:31

6 Answers6

256

In JSX, lower-case tag names are considered to be HTML tags. However, lower-case tag names with a dot (property accessor) aren't.

See HTML tags vs React Components.

  • <component /> compiles to React.createElement('component') (html tag)
  • <Component /> compiles to React.createElement(Component)
  • <obj.component /> compiles to React.createElement(obj.component)
Alexandre Kirszenberg
  • 35,938
  • 10
  • 88
  • 72
  • 14
    Add another half an hour to the counter. I was going insane, trying to render something like `let component = components[compType]; `, and getting nonsense errors. – Zequez Oct 16 '16 at 06:25
  • 2
    I've tried `` which doesn't work either. – Xaree Lee Oct 19 '16 at 01:32
  • 10
    I can't believe that I did not notice there is such a rule before. – shaosh Feb 16 '17 at 00:03
  • 8
    This is a bad idea. I am being polite here. – Rolf Jul 26 '18 at 23:48
  • 3
    Yep this could be extremely off-putting to n00bs since, if calling `Components` instead of `components`, their lovely site will load with no errors but no content! – olisteadman Feb 06 '19 at 10:26
65

@Alexandre Kirszenberg gave a very good answer, just wanted to add another detail.

React used to contain a whitelist of well-known element names like div etc, which it used to differentiate between DOM elements and React components.

But because maintaining that list isn't all that fun, and because web components makes it possible to create custom elements, they made it a rule that all React components must start with a upper case letter, or contain a dot.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Anders Ekdahl
  • 22,685
  • 4
  • 70
  • 59
  • 5
    great info, even better if there's a official doc reference. thanks. – WaiKit Kung Sep 02 '15 at 11:18
  • when was this changed? – nolawi Jul 28 '17 at 23:57
  • Here's an official reference: "We recommend naming components with a capital letter. If you do have a component that starts with a lowercase letter, assign it to a capitalized variable before using it in JSX." https://reactjs.org/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized – James Daily Jul 11 '22 at 14:29
18

From the official React reference:

When an element type starts with a lowercase letter, it refers to a built-in component like or and results in a string 'div' or 'span' passed to React.createElement. Types that start with a capital letter like compile to React.createElement(Foo) and correspond to a component defined or imported in your JavaScript file.

Also note that:

We recommend naming components with a capital letter. If you do have a component that starts with a lowercase letter, assign it to a capitalized variable before using it in JSX.

Which means one has to use:

const Foo = foo; before using foo as a Component element in JSX.

0leg
  • 13,464
  • 16
  • 70
  • 94
7

User define components must be Capitalized

When an element type starts with a lowercase letter, it refers to a built-in component like <div> or <span> and results in a string 'div' or 'span' passed to React.createElement. Types that start with a capital letter like <Foo /> compile to React.createElement(Foo) and correspond to a component defined or imported in your JavaScript file.

React recommend naming components with a capital letter. If you do have a component that starts with a lowercase letter, assign it to a capitalized variable before using it in JSX.

For example, this code will not run as expected:

import React from 'react';

// Wrong! This is a component and should have been capitalized:
function hello(props) {
  // Correct! This use of <div> is legitimate because div is a valid HTML tag:
  return <div>Hello {props.toWhat}</div>;
}

function HelloWorld() {
  // Wrong! React thinks <hello /> is an HTML tag because it's not capitalized:
  return <hello toWhat="World" />;
}

To fix this, we will rename hello to Hello and use <Hello /> when referring to it:

import React from 'react';

// Correct! This is a component and should be capitalized:
function Hello(props) {
  // Correct! This use of <div> is legitimate because div is a valid HTML tag:
  return <div>Hello {props.toWhat}</div>;
}

function HelloWorld() {
  // Correct! React knows <Hello /> is a component because it's capitalized.
  return <Hello toWhat="World" />;
}

Here is the reference

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Malik Haseeb
  • 471
  • 9
  • 20
  • This answer is better because it holds all the information and it it the only one that link to the exact head in the documentation instead of the whole page. The comments in the code example may be a bit much for quick readability so if they would be understandable in a glance this answer would be perfect. (they may help beginners though) – ThaJay Jul 02 '21 at 09:21
6

The first part of a JSX tag determines the type of the React element, basically there is some convention Capitalized, lowercase, dot-notation.

Capitalized and dot-notation types indicate that the JSX tag is referring to a React component, so if you use the JSX <Foo /> compile to React.createElement(Foo)
OR
<foo.bar /> compile to React.createElement(foo.bar) and correspond to a component defined or imported in your JavaScript file.

While the lowercase type indicate to a built-in component like <div> or <span> and results in a string 'div' or 'span' passed to React.createElement('div').

React recommend naming components with a capital letter. If you do have a component that starts with a lowercase letter, assign it to a capitalized variable before using it in JSX.

Umair Ahmed
  • 8,337
  • 4
  • 29
  • 37
5

In JSX, React Classes are capitalized to make XML compatible, so that it is not mistaken for an HTML tag. If the react classes are not capitalized, it is an HTML tag as pre-defined JSX syntax.

PKA
  • 515
  • 5
  • 12