0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>Although JSX tries to be similar to HTML, they have some differences.</p>
1
<p>Although JSX tries to be similar to HTML, they have some differences.</p>
2
<p>In JSX, we write in<em>camelCase</em>all DOM properties and attributes, including event handlers. For instance, the<em>tabindex</em>attribute becomes<em>tabIndex</em>. However, two exceptions to this rule are<em>aria-</em>and<em>data-</em>attributes, which retain their typical spelling as they appear in plain HTML.</p>
2
<p>In JSX, we write in<em>camelCase</em>all DOM properties and attributes, including event handlers. For instance, the<em>tabindex</em>attribute becomes<em>tabIndex</em>. However, two exceptions to this rule are<em>aria-</em>and<em>data-</em>attributes, which retain their typical spelling as they appear in plain HTML.</p>
3
<h2>htmlFor</h2>
3
<h2>htmlFor</h2>
4
<p>Since<em>for</em>is a reserved word in JS, React elements use the<em>htmlFor</em>property.</p>
4
<p>Since<em>for</em>is a reserved word in JS, React elements use the<em>htmlFor</em>property.</p>
5
<h2>Escaping</h2>
5
<h2>Escaping</h2>
6
<p>Standard HTML is not entirely secure, as unescaped text can pose a threat. It is necessary to escape any text that should be displayed as plain text, to prevent any HTML within from executing. This situation can be particularly hazardous in cases where the user adds this text to the website.</p>
6
<p>Standard HTML is not entirely secure, as unescaped text can pose a threat. It is necessary to escape any text that should be displayed as plain text, to prevent any HTML within from executing. This situation can be particularly hazardous in cases where the user adds this text to the website.</p>
7
<p>JSX operates differently. Any output that occurs under normal circumstances is secure by default and is automatically escaped. However, in instances where it is unnecessary, the automatic escaping can be turned off like so:</p>
7
<p>JSX operates differently. Any output that occurs under normal circumstances is secure by default and is automatically escaped. However, in instances where it is unnecessary, the automatic escaping can be turned off like so:</p>
8
<p>See the Pen<a>js_react_jsx_html_difference_safety</a>by Hexlet (<a>@hexlet</a>) on<a>CodePen</a>.</p>
8
<p>See the Pen<a>js_react_jsx_html_difference_safety</a>by Hexlet (<a>@hexlet</a>) on<a>CodePen</a>.</p>
9
<p>You need to use the<em>dangerouslySetInnerHTML</em>property to output without escaping. We pass an object with the __html property to this property, the value of which is a string with HTML.</p>
9
<p>You need to use the<em>dangerouslySetInnerHTML</em>property to output without escaping. We pass an object with the __html property to this property, the value of which is a string with HTML.</p>
10
<p>If a component has a<em>dangerouslySetInnerHTML</em>attribute, it should have no content. The following example will lead to an error:</p>
10
<p>If a component has a<em>dangerouslySetInnerHTML</em>attribute, it should have no content. The following example will lead to an error:</p>
11
<h2>Styles</h2>
11
<h2>Styles</h2>
12
<p>The<em>style</em>attribute works quite differently. In HTML, it's a plain string. In JSX, it's only an object:</p>
12
<p>The<em>style</em>attribute works quite differently. In HTML, it's a plain string. In JSX, it's only an object:</p>
13
<p>See the Pen<a>js_react_jsx_html_difference_style</a>by Hexlet (<a>@hexlet</a>) on<a>CodePen</a>.</p>
13
<p>See the Pen<a>js_react_jsx_html_difference_style</a>by Hexlet (<a>@hexlet</a>) on<a>CodePen</a>.</p>
14
<p>We write CSS property names in<em>camelCase</em>for consistency with attribute names.</p>
14
<p>We write CSS property names in<em>camelCase</em>for consistency with attribute names.</p>
15
<h2>Default property value</h2>
15
<h2>Default property value</h2>
16
<p>If we pass a property to a component without a value, it automatically becomes true.</p>
16
<p>If we pass a property to a component without a value, it automatically becomes true.</p>
17
<p>The examples below are equivalent:</p>
17
<p>The examples below are equivalent:</p>
18
<p>In this case, the first option is preferable.</p>
18
<p>In this case, the first option is preferable.</p>
19
<h2>The rest</h2>
19
<h2>The rest</h2>
20
<p>You can read more about the differences in the<a>official documentation</a>. In addition, future lessons will show these differences in practice.</p>
20
<p>You can read more about the differences in the<a>official documentation</a>. In addition, future lessons will show these differences in practice.</p>