Content deleted Content added
m →Virtual DOM: punct. |
m →JSX: punct., style, fmt. Tag: nowiki added |
||
Line 43:
=== JSX ===
React components are typically written in JSX, a JavaScript extension syntax allowing quoting of HTML and using HTML tag syntax to render subcomponents.<ref>{{Cite web|title = JSX in Depth |url = https://facebook.github.io/react/docs/jsx-in-depth.html|accessdate = 2015-11-17}}</ref> This is a React-specific grammar extension to JavaScript like now defunct [[E4X]]. HTML syntax is processed into JavaScript calls of the React framework. Developers may also write in pure JavaScript. JSX is similar to another extension syntax created by Facebook for PHP, [[XHP]]. JSX looks like regular HTML. An example of JSX code
<syntaxhighlight>
import React from 'react';
Line 65 ⟶ 64:
Even though it is similar to HTML, there are certain aspects you need to keep in mind when working with JSX.
If you want to return more elements, you will need to wrap it in a single container element like the <code><nowiki><div></nowiki></code> element shown above.▼
▲If you want to return more elements you will need to wrap it in a single container element like the <div> element shown above.
You can add your own custom attributes in addition to HTML attributes. The custom attributes need to be added with
▲''Attributes''
▲You can add your own custom attributes in addition to HTML attributes. The custom attributes need to be added with 'data-' prefix
''Javascript Expressions''▼
Javascript expressions can be used inside JSX with curly brackets {}▼
▲Javascript expressions can be used inside JSX with curly brackets <code>{}</code>:
<syntaxhighlight>
import React from 'react';
Line 93 ⟶ 88:
</syntaxhighlight>
The example above will render "11".
We cannot use if else statements inside JSX but we can use ternary expressions instead.▼
In example below browser will render 'true' because i is equal to 1.▼
;Ternary operators
<syntaxhighlight>
import React from 'react';
class App extends React.Component {
var i = 1;
return (
Line 116 ⟶ 109:
export default App;
</syntaxhighlight>
{{sect-stub | JSX syntax is very different
=== Architecture beyond HTML ===
|