Is the entire component a JSX element
Think of anything that looks HTML-ish in React as a JSX element. It’s not HTML of course, it’s JSX. But it does certainly look HTML-ish and can roughly translate to HTML in the end, translated by React.
Why in some places can we write pure JS …
Because React is JS. You are doing JS.
…and in other places we have to encapsulate our JS in curly braces?
Because at that point you are injecting JS into JSX. You are in a JS function, but you are returning/rendering some JSX and you want to inject some JS inside it to have some of the dynamic data in the JSX.
this.state.toDoList.map((item) => {return <li>item</li>})
should be:
this.state.toDoList.map((item) => {return <li>{ item }</li>})
This is JS. Everything is JS, except, you have a JSX element <li>{ item }</li>
. React sits up and says, “Wait a minute JS, there is some JSX here and you don’t know what that is. Don’t worry, I’ll translate it to some JS for you and I’ll do it behind the scenes so you or the programmer don’t have to even know it’s happening.” So React takes a look at <li>{ item }</li>
and says, “OK, I know an li
is just an HTML element, I’ll translate that into some JS so JS can output it properly. But that { item }
is in curly braces. So, the coder doesn’t want the string “item”, they want me to find the variable item back in JS land and insert it here.”
If you had <li>item</li>
, it would just say “item”. But if you have <li>{ item }</li>
, it will show whatever the variable item refers to.
Don’t worry. A few years ago, I was struggling to understand that. Now I’m a professional React Native developer. Just keep at it. There is a lot that is very confusing about this. It is a different way of thinking. But if you keep at it, you will get it. And in the end, it can be incredibly powerful.
Just for emphasis, from your last paragraph:
Is the whole component JSX …
No, just the stuff that looks HTML-ish. Technically you can do React without JSX, but wouldn’t advise it. The component is JS, which may contain some JSX.
… and does the compiler just scan for any html …
My knowledge is a little weaker here, but I think it is the React library that is doing that …
(inside the render function or not)
JSX will usually be in the render or in a function that get’s called in render or a variable that gets injected into JSX (those curly braces) inside render. That is what they mean by “render”.
Also, once it finds some html, it requires curly braces for any JS input?
Essentially. You can have JSX without any dynamic content and you wouldn’t need any curly braces. That is fine. For example, if I just wanted a static header:
class Header extends Component {
render() {
return (
<h1>Welcome to the world's greatest app!!!</h1>
);
};
};
That (<h1>Welcome to the world's greatest app!!!</h1>
) is JSX and has no curly braces because there is no dynamic content, just some static text. But if I wanted to inject some dynamic content, imagine that the username is being sent to the header component.
class Header extends Component {
render() {
return (
<h1>Welcome { this.props.username }, to the world's greatest app!!!</h1>
);
};
};
Now I need curly braces because it is injecting JS into the JSX. The variable this.props.username comes from JS land. That is essentially what makes JSX special - otherwise it would basically be HTML. (That and the fact that you can create your own components.)