You have to return a single root element: If you are working with multiple elements inside of a component, they have to be wrapped in a single parent tag.
This can be div
element.
This can also be a <>
at the start and a </>
at the end.
You have to close all tags: You have to explicitly close all tags.
This means that self closing tags like <img>
become <img />
.
You have to use camelCase for everything: JSX will turn in to JavaScript code and attributes that are written in JSX will become keys of JavaScript objects.
Their names can not contain dashes or be reserved words like `class`.
Like mentioned before, you can use curly braces inside of JSX to add little pieces of JavaScript to your JSX. Some of the time however, it is not necessary.
Usually, when passing a string attribute to JSX, you can just put single or double quotes.
<img alt="Iris van Ollefen" />
But, if you want to make it more dynamic and use a JavaScript value, you can replace the quotes with curly braces.
<img alt={description} />
One: when working with text:
// this will work
<h1>{name}'s To Do List</h1>
// this will not work
<{tag}>Iris van Ollefen</{tag}>
Two: when working with attributes:
// this will work
<img src={avatar} />
// this will read the avatar variable
// this will not work
<img src="{avatar}" />
// this will pass a string {avatar}
You can actually pass more than just strings in JSX. You can even pass objects inside of curly braces.
But, since an object is already inside of curly braces, you will have to use double curly braces.
person={{ name: "Hedy Lamarr", inventions: 5 }}