1

I was trying to make some component , I'm new in React and I tried with some basics like creating simple component that stored <h1></h1> tag with Hello World in it . I noticed that when I'm turning Myfunc's first letter to lowercase "m" it is rendering nothing but empty page although I'm changing it trough the something.js too , I searched in the web and it seems that there's something like rule here , anyway... is there anyway to use component with lowercase ? I'm forgetting that rule many time

import React from "react";
import Myfunc from "./something.js";
function App(){
    return(
       <div>
         <Myfunc/>
       </div>
    );
}
export default App;
JSXStarter
  • 41
  • 9

2 Answers2

0

Here's some part of the docs...maybe from here you can get a better idea.

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.

https://reactjs.org/docs/jsx-in-depth.html

sakshya73
  • 5,570
  • 5
  • 22
  • 41
0

You can have React components with lowercase name, but you can't use them in JSX.

If you strictly don't want to capitalize the first letter, or don't want to reassign it to a capitalized variable ( ReactComp = reactComp ), then you can follow this ugly looking approach.

const myFunc = (props) => {
  const { name, children } = props;
  return <div>
    <h1> {name} </h1>
    <h3> a lower case component </h3>
    {children}
  </div>
}

const App = props => {
  return (
    React.createElement(
      myFunc,
      {
        name: 'My Func',
        // other props
      },
      <h6>This is a child component</h6>,
      <h6>This is another child component</h6>
    )
  )
}

ReactDOM.render(<App />, document.querySelector('#root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="root" />
Avinash Thakur
  • 1,640
  • 7
  • 16