6

I can't seem to access data that's part of an object within an object. here I'm trying to access likes in profile which would otherwise be fine using vanilla javascript to print out this.state.data.profile.likes

class App extends Component {
     constructor(props) {
        super(props);
        this.state = {
            data: {}
        };
     }

     componentDidMount() {
         var x = {      
             "notifications": 12,
             "profile": {
                 "likes": 5
             }
         };
         this.setState({
             data: x
         });
     }


    render() {
        const {notifications, profile} = this.state;
        return (
            <div>
                <span>Notifications {notifications}</span>
                <span>Likes {profile.likes}</span>
            </div>
        );
}
2
  • Aren't you getting the wrong info out of this.state in your render function? I think it would be const { data } = this.state Commented Mar 4, 2018 at 22:23
  • you're right. it should be using { data } Commented Mar 4, 2018 at 22:39

3 Answers 3

2

Before mounting - and on the initial render - your state looks like this:

{
  data: {}
}

After mounting - on the second render - your state looks like this:

{
  data: {
    notifications: 12,
    profile: {
      likes: 5
    }
  }
}

You're trying to access this.state.profile.likes which doesn't exist. Presumably you mean to access this.state.data.profile.likes which does exist, but only on the second render.

Sign up to request clarification or add additional context in comments.

Comments

2

I noticed this while also trying to fix the same problem. The constructor should be:

 constructor(props) {
    super(props);
    this.state = {
        data: {
            profile: {}
        }
    };
 }

Always initialize objects within objects

1 Comment

I had the same issue and this saved me! and yeah you will need to initialize each object, and since profile is another object you will need to initialize it too
0

https://reactjs.org/docs/react-component.html#mounting

render -> componentDidMount

put state data in constructor

5 Comments

It doesn't matter where you place it. As long as the function eventually gets called, it will end up on the DOM.
first - render; second - componentDidMount; this.state.profile.likes in render -- error
You can not access non-existent data. There is a violation of react lifecycle
Oh, you're right in that regard. Trying to access a key in undefined. But I'm pretty sure that's not OPs error.
this was more so an example of using fetch in the mount to get data to populate it into setState so I wouldn't be putting the state data in the constructor. should i be using componentwillmount instead?

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.