FAQ: Your First React Component - Create a Component Class

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “Create a Component Class” exercise from the lesson “Your First React Component”.

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

Web Development

Learn ReactJS: Part I

FAQs on the exercise Create a Component Class

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!

As described in this lesson. React is a JavaScript object which contains methods to use the react. And it also says that React.Component is a class. My question is can an object contain a class as it’s property?

1 Like

I would also like this answered.

@midlindner or @mtf could you maybe help us?

Perhaps someone far more knowledgeable than me with regards to React could answer. @selectall, maybe?

3 Likes

An object can have class instances as the values of their properties, but one doubts that it can have a class as a property. Its parent class can have a parent class that it inherits from, or it can have child classes that inherit from it.

A module can contain multiple classes. React.js is a module, if I’m correct in my understanding.

1 Like

Hello @mariaburmeister & @mauj_mishra,

If I’m understanding your question correctly, then the answer is yes. Here is a small example I wrote to demonstrate and to make sure I’m not misunderstanding.

import React from "react";
import ReactDOM from "react-dom";

class App extends React.Component {
  render() {
    return <h1>Hello, World</h1>;
  }
}

// Store the uninstantiated class in the object
const myClasses = {
  theAppClass: App
};

// JavaScript still lets us extend it using that name if we want
class LoudApp extends myClasses.theAppClass {
  render() {
    return <h1>{this.props.message && this.props.message.toUpperCase()}</h1>;
  }
}

// We can use it in JSX by referencing the object property
ReactDOM.render(
  <div>
    <myClasses.theAppClass />
    <LoudApp message="hello, world" />
  </div>,
  document.getElementById("app")
);

The browser would show:

image

Does this demonstrate what you mean? Please let me know

2 Likes

Thank you @selectall, it does indeed. Sorry for the delay in answering, it just took me some more learning to undestand your example :slight_smile:

Thanks again and have a nice one!

Can I use arrow function syntax to declare function components in React?

1 Like

I was going to ask the exact same thing! I just tried to complete the lesson’s exercise creating the function using the arrow function syntax but it did not accept it as a possible correct answer. It complained about not using the function keyword. :slightly_frowning_face:

I am also wondering whether we can use arrow function syntax in React? I tried to do it in the previous project (Animal Facts) and it didn’t work, so I’m presuming we can’t use it?

1 Like

Thank you! It’s good to know you can use them. It’s weird then that the function in the exercise did not work properly if you used arrow syntax.

1 Like

Not much I can say, but if we take a look at the exercise we might be able to shed some light. Do you have link to that page?

Hello,
In this exercise I had to create a function component. I tried it using the arrow function but that didn’t work. Can someone explain why.

“Don’t put your hand on the stove.”
“Why?”

Arrow functions are not as robust as standard functions and have none of the built-in properties such as, this or arguments, the former being most likely used at some internal level. If the docs say not to use them, then it would do well to follow that advice. No explanation necessary. We just have to accept that it is the way it is.