Jump to content

JSX (JavaScript): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
JSX has nothing to do with DOM–you are thinking of react-dom
Citation bot (talk | contribs)
Added date. | Use this bot. Report bugs. | Suggested by Dominic3203 | Category:Open formats | #UCB_Category 56/117
Line 1: Line 1:
{{Short description|JavaScript syntax extension}}
{{Short description|JavaScript syntax extension}}
'''JSX''' ('''JavaScript XML''', formally '''JavaScript Syntax eXtension''') is an XML-like extension to the [[JavaScript]] language syntax.<ref name=":0">{{cite web|title=Draft: JSX Specification|url=https://facebook.github.io/jsx/|website=JSX|publisher=Facebook|access-date=7 April 2018}}</ref> Initially created by [[Meta Platforms|Facebook]] for use with [[React (JavaScript library)|React]], JSX has been adopted by multiple [[web framework]]s.<ref name=Larsen>{{cite book |last=Larsen|first=John|title=React Hooks in Action With Suspense and Concurrent Mode| year= 2021|publisher= Manning |isbn= 978-1720043997}}</ref>{{rp|5}}<ref name=Wieruch>{{cite book |last=Wieruch|first=Robin|title=The Road to React |publisher= Leanpub|isbn= 978-1720043997}}</ref>{{rp|11}} Being a [[syntactic sugar]], JSX is generally [[transpiler|transpiled]] into nested JavaScript function calls structurally similar to the original JSX.
'''JSX''' ('''JavaScript XML''', formally '''JavaScript Syntax eXtension''') is an XML-like extension to the [[JavaScript]] language syntax.<ref name=":0">{{cite web|title=Draft: JSX Specification|url=https://facebook.github.io/jsx/|website=JSX|publisher=Facebook|access-date=7 April 2018}}</ref> Initially created by [[Meta Platforms|Facebook]] for use with [[React (JavaScript library)|React]], JSX has been adopted by multiple [[web framework]]s.<ref name=Larsen>{{cite book |last=Larsen|first=John|title=React Hooks in Action With Suspense and Concurrent Mode| year= 2021|publisher= Manning |isbn= 978-1720043997}}</ref>{{rp|5}}<ref name=Wieruch>{{cite book |last=Wieruch|first=Robin|title=The Road to React |date=14 September 2018 |publisher= Leanpub|isbn= 978-1720043997}}</ref>{{rp|11}} Being a [[syntactic sugar]], JSX is generally [[transpiler|transpiled]] into nested JavaScript function calls structurally similar to the original JSX.


==Markup==
==Markup==
Line 17: Line 17:


===Nested elements===
===Nested elements===
Multiple elements on the same level need to be wrapped in a single React element such as the <code><nowiki><div></nowiki></code> element shown above, a fragment delineated by <code><nowiki><Fragment></nowiki></code> or in its shorthand form <code><nowiki><></nowiki></code>, or returned as an array.<ref>{{cite web |url=https://reactjs.org/blog/2017/09/26/react-v16.0.html#new-render-return-types-fragments-and-strings |title=React v16.0§New render return types: fragments and strings |last=Clark |first=Andrew |date=September 26, 2017 |website=React Blog}}</ref><ref>{{cite web |url=https://reactjs.org/docs/react-component.html#render |title=React.Component: render |website=React}}</ref><ref name=Wieruch>{{cite book |last=Wieruch|first=Robin|title=The Road to React |publisher= Leanpub|isbn= 978-1720043997}}</ref>{{rp|68-69}}
Multiple elements on the same level need to be wrapped in a single React element such as the <code><nowiki><div></nowiki></code> element shown above, a fragment delineated by <code><nowiki><Fragment></nowiki></code> or in its shorthand form <code><nowiki><></nowiki></code>, or returned as an array.<ref>{{cite web |url=https://reactjs.org/blog/2017/09/26/react-v16.0.html#new-render-return-types-fragments-and-strings |title=React v16.0§New render return types: fragments and strings |last=Clark |first=Andrew |date=September 26, 2017 |website=React Blog}}</ref><ref>{{cite web |url=https://reactjs.org/docs/react-component.html#render |title=React.Component: render |website=React}}</ref><ref name=Wieruch>{{cite book |last=Wieruch|first=Robin|title=The Road to React |date=14 September 2018 |publisher= Leanpub|isbn= 978-1720043997}}</ref>{{rp|68-69}}


===Attributes===
===Attributes===
Line 23: Line 23:


===JavaScript expressions===
===JavaScript expressions===
JavaScript [[Expression (computer science)|expressions]] (but not [[Statement (computer science)|statements]]) can be used inside JSX with curly brackets <code>{}</code>:<ref name=Wieruch>{{cite book |last=Wieruch|first=Robin|title=The Road to React |publisher= Leanpub|isbn= 978-1720043997}}</ref>{{rp|14-16}}
JavaScript [[Expression (computer science)|expressions]] (but not [[Statement (computer science)|statements]]) can be used inside JSX with curly brackets <code>{}</code>:<ref name=Wieruch>{{cite book |last=Wieruch|first=Robin|title=The Road to React |date=14 September 2018 |publisher= Leanpub|isbn= 978-1720043997}}</ref>{{rp|14-16}}
<syntaxhighlight lang="html">
<syntaxhighlight lang="html">
<h1>{10+1}</h1>
<h1>{10+1}</h1>
Line 54: Line 54:
</syntaxhighlight>
</syntaxhighlight>


Functions and JSX can be used in conditionals:<ref name=Wieruch>{{cite book |last=Wieruch|first=Robin|title=The Road to React |publisher= Leanpub|isbn= 978-1720043997}}</ref>{{rp|88-90}}
Functions and JSX can be used in conditionals:<ref name=Wieruch>{{cite book |last=Wieruch|first=Robin|title=The Road to React |date=14 September 2018 |publisher= Leanpub|isbn= 978-1720043997}}</ref>{{rp|88-90}}
<syntaxhighlight lang="jsx">
<syntaxhighlight lang="jsx">
const App = () => {
const App = () => {

Revision as of 08:44, 15 December 2024

JSX (JavaScript XML, formally JavaScript Syntax eXtension) is an XML-like extension to the JavaScript language syntax.[1] Initially created by Facebook for use with React, JSX has been adopted by multiple web frameworks.[2]: 5 [3]: 11  Being a syntactic sugar, JSX is generally transpiled into nested JavaScript function calls structurally similar to the original JSX.

Markup

An example of JSX code:

const App = () => {
   return (
     <div>
       <p>Header</p>
       <p>Content</p>
       <p>Footer</p>
     </div>
   ); 
}

Nested elements

Multiple elements on the same level need to be wrapped in a single React element such as the <div> element shown above, a fragment delineated by <Fragment> or in its shorthand form <>, or returned as an array.[4][5][3]: 68–69 

Attributes

JSX provides a range of element attributes designed to mirror those provided by HTML. Custom attributes can also be passed to the component.[6] All attributes will be received by the component as props.

JavaScript expressions

JavaScript expressions (but not statements) can be used inside JSX with curly brackets {}:[3]: 14–16 

  <h1>{10+1}</h1>

The example above will render:

  <h1>11</h1>

Conditional expressions

If–else statements cannot be used inside JSX but conditional expressions can be used instead. The example below will render { i === 1 ? 'true' : 'false' } as the string 'true' because i is equal to 1.

const App = () => {
   const i = 1;

   return (
     <div>
       <h1>{ i === 1 ? 'true' : 'false' }</h1>
     </div>
   );
}

The above will render:

<div>
  <h1>true</h1>
</div>

Functions and JSX can be used in conditionals:[3]: 88–90 

const App = () => {
   const sections = [1, 2, 3];

   return (
     <div>
       {sections.map((n,i) => (
           /* 'key' is used by React to keep track of list items and their changes */
           /* Each 'key' must be unique */
           <div key={"section-" + n}>
               Section {n} {i === 0 && <span>(first)</span>}
           </div>
       ))}
     </div>
   );
}

The above will render:

<div>
  <div>Section 1<span>(first)</span></div>
  <div>Section 2</div>
  <div>Section 3</div>
</div>

Code written in JSX requires conversion with a tool such as Babel before it can be understood by web browsers.[7][8]: 5  This processing is generally performed during a software build process before the application is deployed.

See also

References

  1. ^ "Draft: JSX Specification". JSX. Facebook. Retrieved 7 April 2018.
  2. ^ Larsen, John (2021). React Hooks in Action With Suspense and Concurrent Mode. Manning. ISBN 978-1720043997.
  3. ^ a b c d Wieruch, Robin (14 September 2018). The Road to React. Leanpub. ISBN 978-1720043997.
  4. ^ Clark, Andrew (September 26, 2017). "React v16.0§New render return types: fragments and strings". React Blog.
  5. ^ "React.Component: render". React.
  6. ^ Clark, Andrew (September 26, 2017). "React v16.0§Support for custom DOM attributes". React Blog.
  7. ^ Fischer, Ludovico (2017-09-06). React for Real: Front-End Code, Untangled. Pragmatic Bookshelf. ISBN 9781680504484.
  8. ^ Larsen, John (2021). React Hooks in Action With Suspense and Concurrent Mode. Manning. ISBN 978-1720043997.