📌
My Docs
React
React
  • General Notes
    • index
    • Review-Of-Previous-Concepts
    • Reiew
    • Spread and Rest
    • Understanding By Example
    • React-Resources
    • Using Web Components in React
    • Thinking in React
    • Hello World
    • REACT ENVIORMENT
    • Components And Props
    • Composition vs Inheritance
    • JSX
    • Advanced
    • Project Examples
    • Node.js versus Next.js - A React Approach
    • Composition vs Inheritance
    • React Components
    • Docs
    • Prerequisites
      • Callbacks
      • Scope
      • Mutability
      • Array-CB-Methods
      • Objects
      • Glossary
    • index
    • Question Set #1:
    • Website
    • Editor Setup
    • Quick Start
    • JavaScript in JSX with Curly Braces
    • Your First Component
    • Reducer
    • Passing Data Deeply with Context
    • Manipulating the DOM with Refs
    • Rendering Lists
    • Choosing the State Structure
    • Tips
  • Udemy React & Redux
    • JSX
    • Modern+React+With+Redux
    • Examples
  • Articles
    • Introduction to React for Complete Beginners
    • A Comprehensive Deep Dive into React
    • Super Simple Intro To React
    • Basic React Tutorial
  • REACT DOCS
    • Shallow Compare
    • Performance Tools
    • Keyed Fragments
    • Test Utilities
    • Code-Splitting
    • Introducing Concurrent Mode (Experimental)
    • Components and Props
    • Concurrent Mode API Reference (Experimental)
    • Conditional Rendering
    • Suspense for Data Fetching (Experimental)
    • Cross-origin Errors
    • Error Decoder
    • Error Boundaries
    • New React App
    • Passing Functions to Components
    • recommended way to structure React projects?
    • Forms
    • Fragments
    • Getting Started
    • Versioning Policy
    • Add-Ons
    • Rules of Hooks
    • Using the State Hook
    • How to Contribute
    • Introducing JSX
    • JSX In Depth
    • Event Pooling
    • Portals
    • Optimizing Performance
    • React Without ES6
    • SyntheticEvent
    • PureRenderMixin
    • ReactDOMServer
    • Profiler API
    • Test Renderer
    • Refs and the DOM
    • Static Type Checking
    • State and Lifecycle
    • Uncontrolled Components
    • Web Components
    • PureRenderMixin
Powered by GitBook
On this page
  • Copy of JavaScript in JSX with Curly Braces
  • Passing strings with quotes
  • Using curly braces: A window into the JavaScript world
  • To Do List for {formatDate(today)}
  • Using “double curlies”: CSS and other objects in JSX
  • More fun with JavaScript objects and curly braces
  • {person.name}'s Todos
  • Recap
  • {person}'s Todos

Was this helpful?

  1. General Notes

JavaScript in JSX with Curly Braces

PreviousQuick StartNextYour First Component

Last updated 3 years ago

Was this helpful?

Copy of JavaScript in JSX with Curly Braces

JSX lets you write HTML-like markup inside a JavaScript file, keeping rendering logic and content in the same place. Sometimes you will want to add a little JavaScript logic or reference a dynamic property inside that markup. In this situation, you can use curly braces in your JSX to open a window to JavaScript.

You will learn

  • How to pass strings with quotes

  • How to reference a JavaScript variable inside JSX with curly braces

  • How to call a JavaScript function inside JSX with curly braces

  • How to use a JavaScript object inside JSX with curly braces

Passing strings with quotes

When you want to pass a string attribute to JSX, you put it in single or double quotes:

export default function Avatar() {

Here, "https://i.imgur.com/7vQD0fPs.jpg" and "Gregorio Y. Zara" are being passed as strings.

But what if you want to dynamically specify the src or alt text? You could use a value from JavaScript by replacing " and " with { and }:

export default function Avatar() {

const avatar = 'https://i.imgur.com/7vQD0fPs.jpg';

const description = 'Gregorio Y. Zara';

src={avatar}

alt={description}

}

Notice the difference between className="avatar", which specifies an "avatar" CSS class name that makes the image round, and src={avatar} that reads the value of the JavaScript variable called avatar. That’s because curly braces let you work with JavaScript right there in your markup!

Using curly braces: A window into the JavaScript world

JSX is a special way of writing JavaScript. That means it’s possible to use JavaScript inside it—with curly braces { }. The example below first declares a name for the scientist, name, then embeds it with curly braces inside the <h1>:

Try changing name’s value from 'Gregorio Y. Zara' to 'Hedy Lamarr'. See how the To Do List title changes?

Any JavaScript expression will work between curly braces, including function calls like formatDate():

const today = new Date();

function formatDate(date) {

return new Intl.DateTimeFormat(

{ weekday: 'long' }

).format(date);

}

export default function TodoList() {

To Do List for {formatDate(today)}

You can only use curly braces in two ways inside JSX:

  1. As text directly inside a JSX tag: <h1>{name}'s To Do List</h1> works, but <{tag}>Gregorio Y. Zara's To Do List</{tag}> will not.

  2. As attributes immediately following the = sign: src={avatar} will read the avatar variable, but src="{avatar}" will pass the string {avatar}.

Using “double curlies”: CSS and other objects in JSX

In addition to strings, numbers, and other JavaScript expressions, you can even pass objects in JSX. Objects are also denoted with curly braces, like { name: "Hedy Lamarr", inventions: 5 }. Therefore, to pass a JS object in JSX, you must wrap the object in another pair of curly braces: person={{ name: "Hedy Lamarr", inventions: 5 }}.

You may see this with inline CSS styles in JSX. React does not require you to use inline styles (CSS classes work great for most cases). But when you need an inline style, you pass an object to the style attribute:

export default function TodoList() {

backgroundColor: 'black',

}}>

  • Improve the videophone

  • Prepare aeronautics lectures

  • Work on the alcohol-fuelled engine

}

Try changing the values of backgroundColor and color.

You can really see the JavaScript object inside the curly braces when you write it like this:

The next time you see {{ and }} in JSX, know that it’s nothing more than an object inside the JSX curlies!

More fun with JavaScript objects and curly braces

You can move several expressions into one object, and reference them in your JSX inside curly braces:

const person = {

name: 'Gregorio Y. Zara',

theme: {

backgroundColor: 'black',

export default function TodoList() {

{person.name}'s Todos

  • Improve the videophone

  • Prepare aeronautics lectures

  • Work on the alcohol-fuelled engine

In this example, the person JavaScript object contains a name string and a theme object:

  name: 'Gregorio Y. Zara',

The component can use these values from person like so:

JSX is very minimal as a templating language because it lets you organize data and logic using JavaScript.

Recap

Now you know almost everything about JSX:

  • JSX attributes inside quotes are passed as strings.

  • Curly braces let you bring JavaScript logic and variables into your markup.

  • They work inside the JSX tag content or immediately after = in attributes.

  • {{ and }} is not special syntax: it’s a JavaScript object tucked inside JSX curly braces.

This code crashes with an error saying Objects are not valid as a React child:

name: 'Gregorio Y. Zara',

backgroundColor: 'black',

export default function TodoList() {

{person}'s Todos

  • Improve the videophone

  • Prepare aeronautics lectures

  • Work on the alcohol-fuelled engine

Can you find the problem?

https://beta.reactjs.org/learn/javascript-in-jsx-with-curly-braces