This site runs best with JavaScript enabled.

What is JSX?

Video Blogger

Photo by Matt Bowden on Unsplash (plus the react_logo)


You may use it every day, but have you seen what happens after Babel compiles it?

I think a critical part of understanding how to use React effectively is understanding JavaScript and JavaScript expressions. So I'm going to show you a few examples of JSX and it's compiled version to help give you an idea of how this all works. As soon as you can compile JSX in your head, you can use the abstraction more powerfully.

Here's our simplest example:

Note, all examples assign to a variable ui just to illustrate that these are regular JavaScript expressions that you can assign to a variable.

1ui = <div id="root">Hello world</div>
2ui = React.createElement('div', {id: 'root'}, 'Hello world')

As shown above, the JSX is compiled to React.createElement. The API to React.createElement is:

1function createElement(elementType, props, ...children) {}
  • elementType can be a string or a function (class) for the type of element to be created
  • props is an object for the props we want applied to the element (or null if we specify no props)
  • ...children is all the children we want applied to the element too. This is just a convenience and we could write an equivalent to above with:
1ui = React.createElement('div', {id: 'root', children: 'Hello world'})

If you have more than one child then you use an array:

1ui = (
2 <div>
3 <span>Hello</span> <span>World</span>
4 </div>
5)
6ui = React.createElement('div', {
7 children: [
8 React.createElement('span', null, 'Hello'),
9 ' ',
10 React.createElement('span', null, 'World'),
11 ],
12})
13
14// Note: babel uses the third argument for children:
15ui = React.createElement(
16 'div', // type
17 null, // props
18 // children are the rest:
19 React.createElement('span', null, 'Hello'),
20 ' ',
21 React.createElement('span', null, 'World'),
22)

What you get back from a React.createElement call is actually a simple object:

1// <div id="root">Hello world</div>
2{
3 type: "div",
4 key: null,
5 ref: null,
6 props: { id: "root", children: "Hello world" },
7 _owner: null,
8 _store: {}
9};

When you pass an object like that to ReactDOM.render or any other renderer, it's the renderer's job to interpret that element object and create DOM nodes or whatever else out of it. Neat right?!

Here are a few more examples for you:

1ui = <div>Hello {subject}</div>
2ui = React.createElement('div', null, 'Hello ', subject)
3
4ui = (
5 <div>
6 {greeting} {subject}
7 </div>
8)
9ui = React.createElement('div', null, greeting, ' ', subject)
10
11ui = <button onClick={() => {}}>click me</button>
12ui = React.createElement('button', {onClick: () => {}}, 'click me')
13
14ui = <div>{error ? <span>{error}</span> : <span>good to go</span>}</div>
15ui = React.createElement(
16 'div',
17 null,
18 error
19 ? React.createElement('span', null, error)
20 : React.createElement('span', null, 'good to go'),
21)
22
23ui = (
24 <div>
25 {items.map(i => (
26 <span key={i.id}>{i.content}</span>
27 ))}
28 </div>
29)
30ui = React.createElement(
31 'div',
32 null,
33 items.map(i => React.createElement('span', {key: i.id}, i.content)),
34)

Notice that whatever you put inside { and } is left alone. This is called an interpolation and allows you to dynamically inject variables into the values of props and children. Because of the way this works, the contents of an interpolation must be JavaScript expressions because they're essentially the right hand of an object assignment or used as an argument to a function call.

Conclusion

If you'd like to play around with this some more, you can try online with Babel's online REPL. Start here. Hopefully this helps you understand a little more about how JSX works and how you can use it more effectively. Good luck!

Learn more about React from me:

Things to not miss:

  • "Headless User Interface Components — "A headless user interface component is a component that offers maximum visual flexibility by providing no interface. "Wait for a second, are you advocating a user interface pattern that doesn't have a user interface?" Yes. That is exactly what I'm advocating." Brilliant article by my friend Merrick Christensen.
  • vscode-go-to-file — A plugin that aims to replicate some of Vim's "go to file" (gf) functionality by the great Jack Franklin
  • tabb — A Chrome extension to search, save, and manage your tabs, history, and bookmarks written in Reason by my friend Ethan Godt
  • deps-report — Generate reports about dependencies and dependents of your JavaScript/TypeScript files through an AST. It supports import and require statements. By the insightful Lorenzo Pichilli.

Discuss on TwitterEdit post on GitHub

Share article
loading relevant upcoming workshops...
Kent C. Dodds

Kent C. Dodds is a JavaScript software engineer and teacher. He's taught hundreds of thousands of people how to make the world a better place with quality software development tools and practices. He lives with his wife and four kids in Utah.