React - Create a Stateful Component

Tell us what’s happening:
Describe your issue in detail here.
Every time I run my answer I get this. * The state of StatefulComponent should be initialized with a property firstName set to a string.

  • Failed:The property firstName in the state of StatefulComponent should render in the h1 element.
    what am I doing wrong?
    Your code so far
    class StatefulComponent extends React.Component {
    constructor(props) {
    super(props);
    // initialize state here

    this.state = {
    name : “Name”
    }

    }
    render() {
    return (


    {this.state.name}



    );
    }
    };


**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36</code>

**Challenge:** React - Create a Stateful Component

**Link to the challenge:**
https://www.freecodecamp.org/learn/front-end-development-libraries/react/create-a-stateful-component

Is this the exact property name the instructions are asking you to use?

I just tried using use your answer of name: “Name” and I still got it wrong

That wasn’t my answer. I didn’t give you an answer. I asked you a question.

Do you have an answer to my question :slightly_smiling_face:

Yes that is the property that they are asking me to use.

if i want to create a myPetName property in my state object, i would do this:

this.state = {
myPetName: “”
}

happy coding.

Your example doesn’t help. The answer should be this.state = {
name : “Name”
} but I don’t know why I fail The state of StatefulComponent should be initialized with a property firstName set to a string? And I fail The property firstName in the state of StatefulComponent should render in the h1 element?

Let me copy the instructions in here:

“Initialize the component with state in the constructor and assign your name to a property of firstName.”

What is the name of the property you are supposed to create?

Also, look at how it is being used in the JSX:

return (
      <div>
        <h1>{this.state.firstName}</h1>
      </div>
    );

You need to have the exact property name in the state if you want to use it in the JSX.

Thank you I just figured out the answer

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.