1

I'm very new to React. I have a component that renders fine, when I try to initialise the state, it says It cannot find the name, as I have not declared it yet, so how do I initialise it properly? I'm using:

"react": "^15.5.4", "react-dom": "^15.5.4",

The component errors here on the line below:

export class Profile extends React.Component<{}, state> {

constructor(props){
    super(props);
    this.state = [{name: 'baz'}, {name: 'shaz'}];
}

public render() {
    return (
        <section>
         <section>
             <h3>profile 1</h3>
             <div>baz</div>
         </section>
            <section>
                <h3>profile 2</h3>
                <div>shaz</div>
            </section>
        </section>
        )

}


 ReactDOM.render(
   >> this is where I call <Profile />,
    document.getElementById('app'),
)

Edit

So I managed to solve the issue with everyones help:

interface State {
    name: string;
}

export class Profile extends React.Component<{}, State> {
    public state: State;

    constructor(props){
        super(props);
            this.state = {
               name: 'baz111'
            };
    }

    public render() {
        return (
            <section>
             <section>
                 <h3>profile 1</h3>
                 <div>baz</div>
             </section>
                <section>
                    <h3>profile 2</h3>
                    <div>{this.state.name}</div>
                </section>
            </section>
        )

    }
}

3 Answers 3

3

this.state is just an object. In this scenario you can run a loop on this.state object to get the value of each name, or if you want you can set all the name under another parent object, for example: let say you want to store names under users key. That would be,

constructor(props){
  super(props);
  this.state = {
    users: [
      {name: 'baz'}, {name: 'shaz'}
    ]
  };
}

Now you should loop on this.state.users to get the name values.

Example,

// Loop using es6 map
componentWillMount() { 
  this.state.users.map( user => {
    console.log( user.name )
  } )
}
Sign up to request clarification or add additional context in comments.

10 Comments

hmm that didn't work either. I got: error TS2304: Cannot find name 'state'.
I have added an example code on how you can get the data
@Jimi you can use this code in componentWillMount to test
I can't access the state as it's not present. So I can't iterate it, access it's objects etc.
Should we use map or forEach?
|
2

As people said already, this.state has to be an object.

I think the real problem is when you initialize your component.

export class Profile extends React.Component<{}, state>

You see that state? That's the default state! As you didn't declare it, that's undefined. Please remove it or set it as void. Or, you can define it outside of your component:

1 - First alternative

export class Profile extends React.Component<{}, void> {
   constructor(props) {
    super(props)
    this.state = {
      users: [/* what you need */]
    }
  }
}

2 - Second alternative

type state {
    {
      users: [/* what you need */]
    }
}

export class Profile extends React.Component<{}, state> {
   constructor(props) {
    super(props)
  }
}

4 Comments

I'm very new to React so trying to cram all this in. State was not defined at all so I could not set it. I guess I could have a constant as the default, like you're suggesting.
Are you using flow for type checking?
your work around seems to get my past the compile error for the undefined param. When I try to access any of the items in state I get this error: error TS2339: Property 'users' does not exist on type 'Readonly<state>'. webpack: Failed to compile.
So try to remove the code <{}, state> from your code base. Adding some type checking has some restrictions that for now, as you are new to React, it would add more complexity. So stick with the first alternative
1

State is an Object.

Write it like this to define the data in state:

this.state = {
   data: [
       {name: 'baz'}, 
       {name: 'shaz'}
   ]
}

Then access the state values by this.state.data.

Update:

Data is an array so you need to use map to print all the values, if you want to print specific value then access that by using the index of that item:

this.state.data[0].name   --->   baz

this.state.data[1].name   --->   shaz

By using map:

this.state.data.map(el => {
   console.log('name', el.name);
})

5 Comments

I realise this, the problem is, no matter how I do this, I get errors. I pasted your code and I got: error TS2304: Cannot find name 'state'.
data is an array, you need to access that by this.state.data[0].name
can you show how you are accessing the name value ?
I can't, state is undefined. Webpack can't compile the component for me to even run it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.