Putting Values Into Markup

What this is. Curly braces switch out of markup and back into JavaScript. Anything that produces a value can go inside them.

The rule that catches people. Braces take an expression, which is something that has a value. They do not take a statement, which is something that performs an action.

{name} works. {count * 40} works. {if (x) ...} does not, because an if statement is not a value. Use a ternary, or calculate it above the return and put the variable in.

Try it

Change done to 2 and watch both the percentage and the message change. One value, several places, always in agreement.

loading editor…

loading React…

✦ What renders and what does not

Strings, numbers and arrays render. null, undefined, false and true render as nothing, which is genuinely useful. Objects throw: "Objects are not valid as a React child" almost always means you wrote {user} where you meant {user.name}.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Attributes, and Why They Are Renamed →
← Back