score:238
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 toreact.createelement('component')
(html tag)<component />
compiles toreact.createelement(component)
<obj.component />
compiles toreact.createelement(obj.component)
score:3
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
score:4
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.
score: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
.
score:16
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.
score:57
@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.
Source: stackoverflow.com
Related Query
- ReactJS component names must begin with capital letters?
- 'A valid React element (or null) must be returned' error with one of the component in ReactJS
- Getting the error: 'A valid React element (or null) must be returned' error with one of the component in ReactJS
- ReactJS - Pass props with Redirect component
- Upload File Component with ReactJS
- ReactJS component not rendering textarea with state variable
- How to avoid 'children with same key' when replacing state in ReactJs component
- How to update ReactJS component based on URL / path with React-Router
- Integrating Facebook Web SDK with ReactJS Component State
- Datatables.net with ReactJS, render a ReactJS component in a column
- Correct way of Creating multiple stores with mobx and injecting it into to a Component - ReactJs
- ReactJS component PropTypes - specify a function type with a set number parameters
- SassError: media query expression must begin with '('
- ReactJS TypeScript pure component with children
- How to use a ReactJS Component with Clojurescipt/Reagent
- How to design a ReactJS component that listens to a WebSocket and interacts with CSS animation
- calling setState on a reactjs component with test utils not re-rendering component
- ReactJs re-use same component but with different styles
- ReactJS children component lost its state when rendered with a different function
- ReactJS Router- How to use history push within function component with parameters?
- ReactJS Styling a Component to take full screen with background color
- creating dynamic component names in react with typescript 3.2.2
- How to add xml to DOM with ReactJS component
- ReactJS Component with "sub-components"
- Unit testing async rendering in ReactJS component with Jest
- Reactjs specific component printing with css
- using conditional component with the same route path in reactjs
- Setting state in a subcomponent of react-table closes component resetting all states with ReactJS
- Reactjs functional component default props with props object
- Styling react-select component with CSS className in Reactjs
More Query from same tag
- Update data in parent when added in child in react
- How to use exif-js in a react Component?
- Create a new unique ID in React JS after users clicks "submit"
- React Error (Only a ReactOwner can have refs.)
- Limit the number of items in menu in react quizzes
- how to re-render after getting axios response in jest test
- material-ui adding search icon in autocomplete component
- Reverse if statement in React and JSX
- Maximum update depth exceeded when updating react state on componentDidUpdate
- Watching state from child component React with Material UI
- how to delete a single item using axios in react
- How to apply a basic transition to a text when hovering over an image in React
- How to properly use react-router-dom No match 404?
- Best (functional) way in React to lift state an arbitrary number of levels?
- Using ternary operator with JSS and Material-UI in React
- 404 Not Found when requesting after deploying to Heroku, works locally
- Multi User login authentication | React Redux
- React.js + Flux -- Passing callback as a prop in view
- Have a container tag with react?
- React JS - React Material UI icons not working?
- useEffect not running Once While Including a dependency Array in reactjs
- Don't use for loop to push to array alternately
- changing Redux state does not affect the render view
- Header flickering and freezing issue with material table (implemented with react window + react window infinite loader)
- ReactJS: show modal with loading message during superagent ajax request
- How to reset password using React and Redux?
- React: Dynamically pass Img Src into Child Component
- Nextjs fatal production only error after adding new package
- How to set PUBLIC_URL back to the server root for npm run build
- How to Break Line in MathJax using ReactJS