Jsx is a javascript extension that allows to write html in javascript.
Note: it is independent from react
Their is a few rules to follow when writing html in jsx that differ from normal html.
You can use a html to jsx converter to convert.
If you want to return multiple tag, wrap everything in a div or with
a fragment: <> </>. A fragment won’t leave trace in the final html.
function Component() {
return (
<>
<h1>text</h1>
<p>hello there</p>
</>
);
}
All tag must be closed, even self closing one.
become
.
Attribute written in kebab-case must be converted in camelCase. The attribute stroke-width become strokeWidth.
The keyword class become className.
Use curly braces {} to inject javascript variable, function… inside
html.
function Component() {
const name = "Bob";
return (
<h1>Hello {name}</h1> // pass the name
)
}
function formatDate(date) {
// format date code here
}
function Component() {
return (
<h1>We are the {formatDate(today)}</h1> // pass the name
)
}