Although JSX tries to be similar to HTML, they do have some differences.
In JSX, all DOM properties and attributes (including event handlers) must be written in camelCase. For example, the tabindex attribute turns into tabIndex. The exceptions are aria- and data- attributes; they're written in exactly the same way as in plain HTML.
Since for is a reserved word in JS, React elements use the htmlFor property.
Ordinary HTML isn't very secure. Any text that needs to remain as text must be escaped before output. Otherwise, if there's HTML inside, it will be interpreted. The situation can become dangerous if this text is added to the site by users themselves.
JSX works differently. Anything that's output in the normal way is safe by default and is automatically escaped. But in places where it isn't required, the escaping is turned off like so:
See the Pen js_react_jsx_html_difference_safety by Hexlet (@hexlet) on CodePen.
Basically, you need to use the dangerouslySetInnerHTML property to output without escaping. An object with the __html
, property is passed to this property, the value of which is a string with HTML. Note: If a component has a dangerouslySetInnerHTML attribute, it should have no content. The following example will lead to an error:
<div dangerouslySetInnerHTML={{ __html: '<p>content</p>' }}>more content</div>;
The style attribute works quite differently. Although in HTML it's a plain string, in JSX it's only an object.
See the Pen js_react_jsx_html_difference_style by Hexlet (@hexlet) on CodePen.
For consistency with attribute names, CSS property names must also be written in camelCase.
If a property is passed to a component without a value, it automatically becomes true
.
The examples below are equivalent:
<MyTextBox autoComplete />
<MyTextBox autoComplete={true} />
In this case, the first option is preferable.
You can read more about the differences in the official documentation. In addition, future lessons will show these differences in practice.
The Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From a novice to a developer. Get a job or your money back!
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.