FAQ: this.props - Render a Component's props

This community-built FAQ covers the “Render a Component’s props” exercise from the lesson “this.props”.

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

Web Development

Learn ReactJS: Part I

FAQs on the exercise Render a Component’s props

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!

Hi

Regarding: Render a Component’s props

I am not getting the idea where it can be used or will help in real example. can someone please explin me in simple way please.

Thanks

What am I supposed to be learning from writing the reactdom line in every single excercise? is it so hard to pre place it on the excercises?

I think the point of this exercise was to show the string interpolation use like when you have something like this:

var cat =‘meow’
var string = The cat says ${cat};

I think they are showing that usage but with this/JSX/React stuff… Though I will say step two which was write a new name to pass the exercise felt like busy work and not really pushing the concept further. This unit overall might need a bit of a tweak.

there should be back ticks before the dollar sign and then after the closing curly brace but for some reason didn’t show

The backticks don’t show because they are used in Markdown to denote inline code blocks, similar to using <code></code> tags in HTML.

In order to render the backticks they must be preceded by a backslash ( \ ). So in your example it could be written as so:
var string = The cat says `${cat}`;

An alternative is to use three backticks ( ``` ) before and after code to denote a code block:

var cat = 'meow';
var string = The cat says `${cat}`

Hope this helps clear it up a bit. Markdown can be very useful to easily add some basic formatting like lists, headings, quotes etc. I use it for taking notes as it allows me to format all my information like it is one large HTML document, without the cumbersome opening and closing tags.

GLHF

in the following code, the define the first name (Groberta), after declaring the new react component which involves the first name. why isn’t there an error? why does this work? Is it because of hoisting?

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

class Greeting extends React.Component {
render() {
return

Hi there, {this.props.firstName}!

;
}
}

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

Hello, i have a question.
In this example:

const myFunction = (myNum) => {
return myNum +1;
}
console.log(myFunction(myVar));
let myVar = 1;

// ReferenceError: Cannot access ‘myVar’ before initialization

We get a reference error because the variable is called after console.log.

Why is the exercise example different?

The prop is declared AFTER the expression: {this.props.firstName}

Ok after 1 second i though myself the answer…its the ReactDOM.render which calls the class…so no inverted order :stuck_out_tongue:

Which is the best practice?

To access props by destructuring them?

import React from 'react';

function Product({name, price, rating}) {
  return (
    <div>
      <h1>{name}</h1>
      <h2>{price}</h2>
      <h3>{rating}</h3>
    </div>
  );
}

export default Product;

Or just by passing props as a parameter?

import React from 'react';

function Product(props) {
  return (
    <div>
      <h1>{props.name}</h1>
      <h2>{props.price}</h2>
      <h3>{props.rating}</h3>
    </div>
  );
}

export default Product;

It is not necessary to call it “props”. You can use any name for the function parameter.

function Product(p) {
  return (
    <div>
      <h1>{p.name}</h1>
      <h2>{p.price}</h2>
      <h3>{p.rating}</h3>
    </div>       
  );
}

That is, to inject tag values into the Product() function, which is located in the Product.js file, I have to switch and watch another App.js file? Because it is in the App.js we create attributes and assign values to <Product />.
And if there are 20 attributes? It’s not really convenient.
Can I somehow write <Product /> attributes in a file Product.js ?