JSX (programming)

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

JSX is an XML-like extension to the JavaScript language syntax. Initially created by Facebook for use with React, JSX has been adopted by multiple web frameworks. Being syntactic sugar, JSX is generally transpiled into nested JavaScript function calls structurally similar to the original JSX. When used with TypeScript, the file extension is .tsx.

JSX is an XML-like extension to the JavaScript language syntax. Initially created by Facebook for use with React, JSX has been adopted by multiple web frameworks. Being syntactic sugar, JSX is generally transpiled into nested JavaScript function calls structurally similar to the original JSX. When used with TypeScript, the file extension is .tsx.

Markup

The following examples include both the React-JSX/TSX code and the transpiled output from Babel. const App = () => { return React.createElement("div", null, React.createElement("p", null, "Header"), React.createElement("p", null, "Content"), React.createElement("p", null, "Footer")); }; export default App; The classic interface remains commonly used by non-React users of JSX.

Nested elements

Multiple elements on the same level need to be wrapped in a single element such as the element shown above, a fragment delineated by or in its shorthand form <> , or returned as an array.

Attributes

JSX provides a range of element attributes designed to mirror those provided by HTML. Custom attributes can also be passed to the component. All attributes will be received by the component as props (properties). Valid JSX attribute names are valid JSX identifiers (variable names), which differ from ECMAScript identifiers mainly by additionally allowing the - (hyphen-minus) character in a non-starting position. This allows HTML attributes with dashes such as data-* and aria-role to be used easily. Attributes still with invalid characters may be notated with the {... spread} syntax.

Custom components

Custom "tags" (components) are simply JavaScript functions that accept a specific calling style. Here name="Connor" simply becomes an option { name: "Connor" } . Components that contain wrap components can be defined using the children attribute that we've already seen in the transpiled output: Valid JSX tag names are valid JSX identifiers (variable names), which mean that hypenated XML and HTML tags can be used without issue. However, custom components cannot have hyphenated names due to limitation from ECMAScript grammar.

JavaScript expressions

JavaScript expressions (but not statements) can be used inside JSX with curly brackets {} : The example above will render: 11

Conditional expressions

If–else statements cannot be used inside JSX but conditional expressions can be used instead. The example below will render as the string 'true' because i is equal to 1. The above will render: true Functions and JSX can be used in conditionals: The above will render: Section 1 (first) Section 2 Section 3 The HTML style attribute applies a collection of CSS styles to the element with it. As an ergonomic measure, React-DOM not only accepts passing this value in string form, but also as a JavaScript object. Instead of , one could write {{code|2=jsx|1= { color: clr } }>}}. The implementation of the "style" function automatically translates the object to the correct string format. A similar camelcase-renaming operation is done to simplify the use of styles with hyphenated names. React-DOM also allows passing JavaScript functions instead of strings as values for the event handler attributes.

See also

ECMAScript for XML (E4X)

References

External links

, Draft: JSX specification

Text derived from https://en.wikipedia.org/w/index.php?oldid=1376319299 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

15 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.