You embed JavaScript expressions in JSX by wrapping them in curly braces: {expression}. Anything that produces a value works, including variables, function calls, arithmetic, template literals and ternaries. The braces can appear as the content between tags or as the value of an attribute. This is how JavaScript expressions in JSX turn static markup into a dynamic UI.
Only expressions are allowed inside the braces. Statements such as if, for and const are not, and later sections show the replacements.
&&Put braces where a value is expected:
Notes on this example:
src={imageUrl} replaces quotes with braces. Never write src="{imageUrl}", which sets the literal text.width={64} need braces, because width="64" would pass a string.alt text.50 - user.score and new Date().Functions are expressions too, so you can call them directly, or write the logic above the return for readability:
For <Product name="Notebook" cents={1250} /> the output is NOTEBOOK: $12.50. Keep long calculations out of the markup and store the result in a variable first.
&&An if statement is not allowed inside braces, so use the conditional operator, or compute the value before returning:
condition && <Element /> renders the element when the condition is truthy. Be careful with numbers: {count && <List />} renders a literal 0 when count is 0, because React renders the number 0. Write {count > 0 && <List />} instead.
For several branches, early returns above the JSX are often clearer:
Turn an array into elements with .map() and give each item a stable key:
Use a unique id from your data as the key. The array index is acceptable only for static lists that never reorder.
Not every value shows up on the page. Check this table when something is missing.
| Value | What React renders |
|---|---|
| String or number | The text |
true, false, null, undefined | Nothing |
| An array | Each item in order |
| A React element | The element |
A plain object such as { a: 1 } | Error: objects are not valid as a React child |
A Date object | Error; convert it first, for example date.toISOString() |
Booleans render nothing, which is what makes condition && <Element /> work. If you want to display true on screen, convert it with String(value).
To debug a value, you can temporarily show an object with {JSON.stringify(user)} or read it in the browser console.
Event handlers and props are also expressions. Pass a function, not the result of calling it:
onClick={onSave("draft")} would call onSave while rendering, which is a classic bug. Wrap the call in an arrow function, or pass onSave itself when it needs no arguments.
No, only expressions are allowed. Use a ternary, &&, an early return, or compute the value in a variable before the return.
React renders the number 0. Compare explicitly, as in {items.length > 0 && <List />}, or use a ternary that returns null.
Use {/* comment */} inside the children. A plain // comment would show up as text.
You cannot render a promise directly in a client component. Fetch data in an effect or with a data library, store the result in state, and render that. Server Components can be async, but that is a framework feature.
Use {} anywhere in JSX to embed a JavaScript expression, as text or as an attribute value. Use ternaries and && for conditions, .map() with a stable key for lists, and remember that booleans, null and undefined render nothing while plain objects throw an error. Pass functions to handlers instead of calling them in the markup.