Why do React components need to start with capital letters?
Bhat Aasim

Bhat Aasim @bhataasim

About: Currently Looking for a JOB | Full Stack Engineer| Part time Bug Hunter

Location:
Kashmir
Joined:
Apr 23, 2024

Why do React components need to start with capital letters?

Publish Date: Sep 21 '24
223 57

A quick explanation of why React components need to start with capital letters. Learn how React differentiates between native HTML elements and custom components.

Why do React components need to start with capital letters?

If you’ve ever worked with React, you might have noticed that component names always start with capital letters. But do you know why? 🤔

//This is wrong and will not work ❌
export function myComponent() {
  return (
        <h1>Hello, World!</h1>
    );
}
Enter fullscreen mode Exit fullscreen mode
//Right way to write a React component ✅
export function MyComponent() {
  return (
        <h1>Hello, World!</h1>
    );
}
Enter fullscreen mode Exit fullscreen mode

In JSX, React components are written in a syntax that gets transformed into plain JavaScript using the React.createElement API, thanks to Babel. Here’s where the capital letter comes in:

When Babel encounters a name starting with a capital letter, it knows it’s dealing with a React component and converts it into a React Fiber object (a key part of React’s rendering system).

On the other hand, if the name starts with a lowercase letter, Babel treats it as a string rather than a component. This helps React differentiate between native HTML elements and custom components!

So, always remember to capitalize your component names for React to interpret them correctly. 💡

Thanks for reading! 🚀

Comments 57 total

  • Kudzai Murimi
    Kudzai MurimiSep 21, 2024

    Ahhhha nice!

  • Carlos Henrique Leal
    Carlos Henrique Leal Sep 21, 2024

    Very good. Thank you!

  • Mohit
    MohitSep 22, 2024

    Good information, thanks for providing

  • Rehan
    RehanSep 22, 2024

    Thank you 👍

  • SkySung
    SkySungSep 22, 2024

    Short and informative thanks!

  • Alexander
    AlexanderSep 22, 2024

    What about const SomeName = "some value" and projects without Babel?

    • Bhat Aasim
      Bhat AasimSep 22, 2024

      SomeName simply a variable, not a react component. Capitalization rule will not apply here, because its not being used as a react component here.

      Projects without babel, then React.createElement() Api is used Explicitly.

      BTW, using Capital letters in Components, helps react to differentiate between Native Elements and custom Components.

      Eg.

      React.createElement('div'); 
      // Interpreted as a native HTML <div> element
      
      React.createElement(MyComponent); 
      // Interpreted as a custom React component
      
      Enter fullscreen mode Exit fullscreen mode

      If MyComponent is written in lower case (myComponent), React will look for a native HTML element <myComponent> which doesn't exists, causing the app to fail.

      • Alexander
        AlexanderSep 22, 2024

        I see the point with createElement, but I still can write lower case functional component on a project without Babel and still get the error. So it's not Babel concern, as your article states

        • Bhat Aasim
          Bhat AasimSep 23, 2024

          You're absolutely right! The capitalization rule in React isn't tied specifically to Babel, but rather to React itself. While Babel helps by converting JSX into React.createElement() calls.

          • Alexander
            AlexanderSep 23, 2024

            This is not true either since React 17 jsx runtime

      • David Sherman
        David ShermanSep 23, 2024

        I’m fairly sure for this they just check whether typeof input is string.

  • six
    sixSep 22, 2024

    Concise and to the point. I love this!

  • Rohit Raj
    Rohit RajSep 22, 2024

    New information, thank.❤️

  • Bhat Aasim
    Bhat AasimSep 23, 2024

    Thanks ❣️

  • Naveed Ullah
    Naveed UllahSep 23, 2024

    To the point and informative which clears the basic concept, thank you

  • Josef Nobach
    Josef NobachSep 23, 2024

    Thanks good to remember!

  • Alpesh Bhagwatkar
    Alpesh BhagwatkarSep 23, 2024

    Short and sweet!

  • cmohanc
    cmohancSep 23, 2024

    Fragile tech

  • ashishnimrot
    ashishnimrotSep 23, 2024

    Could you tell me cons of virtual dom in react js?

    • Bhat Aasim
      Bhat AasimOct 6, 2024

      I don't think there are any cons in React.js Virtual DOM. But these could be:

      • Memory Usage
      • Diffing Complexity
  • Sean Dinan
    Sean DinanSep 23, 2024

    That's good to know! Had always assumed it was just a matter of convention, although it would feel very strange to not capitalize a React component at this point :)

    • Kelvin Macharia
      Kelvin MachariaDec 13, 2024

      I know. The convention just sticks
      This post was necessay just to know why

  • CitronBrick
    CitronBrickSep 23, 2024

    On a side note, Kotlin's Composable function components for Android, also start with a capital letter.

  • Prince Malongo
    Prince MalongoSep 23, 2024

    Thanks for the insight from the article and the informative comments. I've have learned and it's a good step to go and study more on the topic.

  • Fathima Irfana
    Fathima IrfanaSep 24, 2024

    Good recap and informative too. Thanks

  • Florian Rappl
    Florian RapplSep 24, 2024

    Has nothing to do with React. It's a JSX assumption to distinguish between a built-in (string) and a reference. Plus you don't need it - if it's a member (contains a dot) it will also be taken as a reference.

    • Web-Dev-Codi
      Web-Dev-CodiOct 4, 2024

      False

      • Florian Rappl
        Florian RapplOct 4, 2024

        Care to elaborate?

        Maybe try it out first, before writing something:

        1. JSX is independent of React (true - it's origins are in React, but it was decoupled very early on and can be used with whatever - even you can just roll your own JSX factory)
        2. <foo.bar /> resolves to h(foo.bar) and not h("foo.bar") - so I am not sure if have some experience with JSX or just like to troll.
      • Max Harris
        Max HarrisOct 11, 2024

        Image description

  • avelops
    avelopsSep 24, 2024

    It was pretty obvious to me, thanks for laying it out and validating my assumptions

  • Vaishnavi Nagvanshi
    Vaishnavi NagvanshiSep 24, 2024

    Thanks for this informative article

  • Joel Angel
    Joel AngelSep 24, 2024

    thanks

  • serkan cakmak
    serkan cakmakSep 24, 2024

    Thank you for sharing useful information

  • Shailendra Singh
    Shailendra SinghSep 26, 2024

    nyc information

  • Tomasz Bernat
    Tomasz BernatSep 27, 2024

    Quick and to the point. Upvote!

  • Web-Dev-Codi
    Web-Dev-CodiOct 4, 2024

    The comments are always like, well actually It is blah blah blah.

    That translates to, oh, I know something different. Let me sh*t on this post.

    The fun part is usually they're wrong.

Add comment