DEV Community

Cover image for Explain JSX like I'm five.
Helitha Rupasinghe
Helitha Rupasinghe

Posted on

Explain JSX like I'm five.

Oldest comments (28)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

An abomination

Collapse
 
hr21don profile image
Helitha Rupasinghe

Very informative! πŸ‘

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

πŸ‘

Collapse
 
hanufu profile image
Eduardo Ribeiro

lol

Collapse
 
posandu profile image
Posandu • Edited

JSX is just fancy HTML (With some rules) that compiles to pure JavaScript. So if you write something like this:

 <div>
    <h1>Hello World</h1>
 </div>
Enter fullscreen mode Exit fullscreen mode

It will compile to something like this:

React.createElement("div", null, React.createElement("h1", null, "Hello World"));
Enter fullscreen mode Exit fullscreen mode

So now you can generate HTML with JavaScript. This is the basic idea behind JSX.

Collapse
 
hr21don profile image
Helitha Rupasinghe

Great example! πŸ‘

Collapse
 
codesimple profile image
code-simple

πŸ‘Welldone

Collapse
 
allisongarner profile image
AllisonGarner

JSX stands for JavaScript XML. It is mostly used with React to describe what the UI should look like.

Collapse
 
hr21don profile image
Helitha Rupasinghe

Love this! πŸ”₯

Collapse
 
lexlohr profile image
Alex Lohr

JS is a language describing logic and interaction. HTML is a language describing content, based on XML, which is meant to describe arbitrary data.

JSX, a shorthand for JS+XML, is these two languages put together into one. In JS, you can use numbers, strings, etc, but HTML code would cause errors. In JSX, HTML Code becomes a valid value that represents the actual HTML.

Collapse
 
matijanovosel profile image
Matija Novosel • Edited

JSX is a combination of HTML and Javascript to put it simply, or as the official React site says: "a syntax extension of Javascript". It offers the functionality of writing Javascript expressions inside HTML elements which is then processed to create the UI.

e.g. this...

const navBar = (<nav className="bg-yellow"> Home </nav>);
Enter fullscreen mode Exit fullscreen mode

... gets converted into:

const navBar = React.createElement("nav", { className: "bg-yellow" },  "Home");
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hr21don profile image
Helitha Rupasinghe

Thank you for sharing!

Collapse
 
yosipmikecolin profile image
Yosip Mike Colin

Se denomina JSX a la combinaciΓ³n de HTML y JS para dar lugar a un cΓ³digo mucho mas compacto y elegante.

Collapse
 
hr21don profile image
Helitha Rupasinghe

bien dicho hermano

Collapse
 
pengeszikra profile image
Peter Vivo • Edited

Fo me JSX is a first selling pont of react (and nextjs). Because I can create any HTML releated stuffs at no time with simple js (ts) function, without need to know any template language syntax. I can use any js controll sequence for put together the view.

JSX also give good abstraction for view declaration.

// nextjs also can declare head elements

const FooGameFrame = () => {
  const [state, dispatch] = useSagaReducer(mainSaga, gameReducer, initialState);
  const army = getDispatchedActions(getActionsLookup(), dispatch);
  const {game, hero} = state;

  return (
    <section className='portal-root m-2'>
      <MobilFrame>
        <Head>
          <title>Foo Game</title>
          <meta name="viewport" content="initial-scale=1.0, width=device-width" />
          <link rel="manifest" href="manifest.json"></link>
        </Head>

        {game === GameMode.ROLL_CHARACTER && <CreateHero state={state} army={army} />}
        {game === GameMode.ADVENTURE_ON_MAP && <SingleAdventure state={state} army={army} />}
        {game === GameMode.BLOG && <Blog name={hero?.name} avatar={hero?.avatar} />}
      </MobilFrame>
      {game === GameMode.ADVENTURE_ON_MAP && <CombatZone state={state} army={army} />}
    </section>
  );
}; 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hr21don profile image
Helitha Rupasinghe

Certainly!

Collapse
 
zhouyg profile image
zhou-yg

JSX is a syntax candy created by React

Collapse
 
jamescurran profile image
James Curran

JSX takes HTML, a simple straightforward way to describe formatted text, and made it verbose, complex and confusing.