ReactJS component names must begin with capital letters?

后端 未结 5 2097
忘了有多久
忘了有多久 2020-11-22 04:56

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 instan

5条回答
  •  醉酒成梦
    2020-11-22 05: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.

提交回复
热议问题