FAQ: Advanced JSX - Variable Attributes in JSX

This community-built FAQ covers the “Variable Attributes in JSX” exercise from the lesson “Advanced JSX”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn ReactJS: Part I

FAQs on the exercise Variable Attributes in JSX

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’m currently stuck with the error message “gooseImg should be equal to an element.”
My code:

import React from ‘react’;
import ReactDOM from ‘react-dom’;

const goose = ‘https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-goose.jpg’;

// Declare new variable here:
const gooseImg = (

);

My variable gooseImg seems to be equal to an img element. Am I missing something or is this a bug?

import React from ‘react’;
import ReactDOM from ‘react-dom’;

const goose = ‘https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-goose.jpg’;

// Declare new variable here:
const gooseImg = ();

ReactDOM.render(gooseImg, document.getElementById(“app”));

According to codes above, why at ReactDOM.render’s first parameter,
gooseImg does not need curly braces?

Why doesn’t this work for the first step?

const gooseImg = (

);

Like it definitely does work and even follows your example, and after adding the rest from part 2 it correctly displays the image?

const gooseImg = (

);

ReactDOM.render(gooseImg, document.getElementById(‘app’));

I also had another issue in a previous course where I missed a comma, but it said I passed (green check box) even though it didn’t display the 2 + 3??? Do you guys test these courses?

It’s worth mentioning when curly braces are used to set attributes, the original quotes are dropped.
While this is obvious in 7th step (all attributes are string), it’s the same for event handlers.

<... onclick="handler();"...>

becomes

<... onClick={handler}...>
4 Likes
const gooseImg = <img src={goose} />;

You have to put the img inside,
and write it in one line, otherwise it doesnt accept it.
What is a bit stupid, as in the last exercise it was written that if we put it on more lines its more readable.

4 Likes

Yes, exactly, I assumed it is just this practice that does not accepts this syntax even though it is correct.

You need to write it on one line which is sort of dumb when the exercise tells us to do multiple lines to make it look more legible.

2 Likes

Yeah that threw me because my instinct was to write:

const gooseImg = <img src="{goose}" />;

…which threw an error. It would have been nice for them to say why this is the case.

Agree - the exercise leads you towards putting it on multiple lines… bit silly.

Also arent you supposed to wrap JSX in parenthesis? All the examples say you need to and then the code throws an error when you put parenthesis. mmm

2021 and this is still an issue.

You don’t have to use parenthesis () if you write it all in a single line:

const gooseImg = <img src={goose}>;

However, I prefer doing it this was as it’s more readable:

const gooseImg = (
  <img src={goose} />
);

Weirdly enough, even though the lesson suggest putting attributes on its own line, this wont run:

const gooseImg = (
  <img
    src={goose} />
);

But that’s just a bug. It runs fine outside the Codecademy environment.

2 Likes

Why do not we include double quotes around JS expressions when assigning attributes?

const gooseImg = <img src="{goose}">;

Hey, @bartusari4466593405 welcome to the forums.

In JSX you do put quotes (single or double) around attributes. But if the value of the attribute is from a variable or object you don’t since its not a string.

thanks but the expression of the curly braces return without double quotes. that is why I assumed we should include double quotes to validate jsx attibutes

Do I need to put the final slash in the self-closing tag of a JSX element? I know it is optional in JS, but what about JSX?

See the lesson: Self-Closing Tags

1 Like