--- id: jsx-in-depth title: JSX In Depth permalink: docs/jsx-in-depth.html redirect_from: - "docs/jsx-spread.html" - "docs/jsx-gotchas.html" - "tips/if-else-in-JSX.html" - "tips/self-closing-tag.html" - "tips/maximum-number-of-jsx-root-nodes.html" - "tips/children-props-type.html" - "docs/jsx-in-depth-zh-CN.html" - "docs/jsx-in-depth-ko-KR.html" --- Fundamentally, JSX just provides syntactic sugar for the `React.createElement(component, props, ...children)` function. The JSX code: ```js Click Me ``` compiles into: ```js React.createElement( MyButton, {color: 'blue', shadowSize: 2}, 'Click Me' ) ``` You can also use the self-closing form of the tag if there are no children. So: ```js
``` compiles into: ```js React.createElement( 'div', {className: 'sidebar'} ) ``` If you want to test out how some specific JSX is converted into JavaScript, you can try out [the online Babel compiler](babel://jsx-simple-example). ## Specifying The React Element Type {#specifying-the-react-element-type} The first part of a JSX tag determines the type of the React element. Capitalized types indicate that the JSX tag is referring to a React component. These tags get compiled into a direct reference to the named variable, so if you use the JSX `` expression, `Foo` must be in scope. ### React Must Be in Scope {#react-must-be-in-scope} Since JSX compiles into calls to `React.createElement`, the `React` library must also always be in scope from your JSX code. For example, both of the imports are necessary in this code, even though `React` and `CustomButton` are not directly referenced from JavaScript: ```js{1,2,5} import React from 'react'; import CustomButton from './CustomButton'; function WarningButton() { // return React.createElement(CustomButton, {color: 'red'}, null); return ; } ``` If you don't use a JavaScript bundler and loaded React from a `