Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do components in react need to be capitalized? [duplicate]

So, when you declare a component in react with a lowercase first letter, it doesn't show up, without throwing an error. When you capitalise the component name, it does work.

What is it implemented like this? To avoid collision with existing html elements, or is this a bug?

var test = React.createClass({
  render: function() {
    return (
      <div>Test</div>
    );
  }
});

var Screen = React.createClass({
  render: function() {
    return (
      <div>
        <test/>
      </div>
    );
  }
});

When I change test to Test, it works:

var Test = React.createClass({
  render: function() {
    return (
      <div>Test</div>
    );
  }
});

var Screen = React.createClass({
  render: function() {
    return (
      <div>
        <Test/>
      </div>
    );
  }
});
like image 712
ngstschr Avatar asked Oct 21 '15 12:10

ngstschr


People also ask

Why do React components need to be capitalized?

User-Defined 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.

Should React components files be capitalized?

Using Pascal Case for Both File and Component Name In addition, it requires the first letter to be uppercase as well, while the camel case does not.

Can React components be lowercase?

All React component names must start with a capital letter. If you start a component name with a lowercase letter, it will be treated like a built-in element like a <div> or a <span> . This is because of the way JSX works. In JSX, rendering a component that begins with a lowercase letter compiles down to React.

Should React be capitalized?

Therefore, your custom React components must either start with a capital letter or use the dot notation. If, for some reason, you have a React component that does start with a lowercase letter, then you should assign it to a capitalized variable before using it in JSX.


1 Answers

From some react release notes

the JSX tag name convention (lowercase names refer to built-in components, capitalized names refer to custom components).

like image 59
Quentin Avatar answered Sep 27 '22 23:09

Quentin