JavaScript React Basics First Steps in React Embed JavaScript Expressions in JSX

Jesse Dispoto
Jesse Dispoto
Front End Web Development Techdegree Graduate 14,537 Points

Write the const variable names inside the header tag instead of copying and pasting the h1 and p elements?

While rewriting our code to use JSX, Gil manually copies and pastes the h1 and p elements inside the header JSX element like so

 const header = (
    <header>
        <h1> My First React element</h1>;
        <p> I just learned how to create a react node and render it into the dom</p>;
    </header>
);

This seems somewhat counter intuitive. Is there a way were we can simply write the variable names inside the header tag, like so?

 const header = (
    <header>
        title
        desc
    </header>
);
Jesse Dispoto
Jesse Dispoto
Front End Web Development Techdegree Graduate 14,537 Points

answered my own question... assign the variables to plain js strings and then put the variables inside the respective HTML tags within the {}

1 Answer

Ezra Siton
Ezra Siton
12,624 Points

For other users:

const title = "my title";
const desc = "my desc";

const header = (
  <header>
    <h1>{title}</h1>
    <p>{desc}</p>
  </header>  
);

ReactDOM.render(
  header,
  document.getElementById('root')
);