1

I'm trying to pass and array of objects, fetched fro get request, to component's state, which has an array. I can successfully pass an array of numbers, or really anything but objects cause when i do that i recieve this: error_image.png

Check out my .js files below, thanks.

comment_item.js :

import React from 'react';
import axios from 'axios';

export class CommentItem extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      comments: []
    };
}

componentWillMount() {
  axios.get('api/v1/trip/2/comment/')
    .then(response => {
      const comments = [{a: 2, b: 4}, {a: 1, b: 3}]; //response.data should be here, but for your convenience I've put array of objects
      this.setState({comments});
    });
}

render() {
    return (
      <div>
        <ul>
            <li>
                {this.state.comments[0].a}
            </li>
        </ul>
      </div>
    );
  }
}

comment.js :

import React from 'react';

import Paper from 'material-ui/Paper';
import Divider from 'material-ui/Divider';
import List from 'material-ui/List/List';

import {CommentItem} from './comment_item';
import {CommentForm} from './comment_form';

const styles = {
  paper: {
      paddingLeft: 15,
      paddingRight: 15,
      paddingBottom: 15,

      marginLeft: 15,
      marginRight: 15,
      marginBottom: 15
  },

  divider: {
      backgroundColor: 'grey'
  }

};

export default class Comment extends React.Component {
    render() {
        return (
            <Paper zDepth={5} rounded={false} style={styles.paper}>
                <div>
                    <List>
                        <CommentItem />
                    </List>

                    <Divider style={styles.divider}/>
                    <CommentForm/>
                </div>
            </Paper>
        );
    }
}

3 Answers 3

1

While rendering, this.state.comments isn't yet available, that's why you are getting undefined error. Try to add a condition, which will tell react to render given comment only if the state.comments variable is available.

<li>
   { this.state.comments.length ? this.state.comments[0].a : null }
</li>
Sign up to request clarification or add additional context in comments.

4 Comments

First of all thanks for your reply :) I've tried to copy-paste your answer into my code and refresh the page, but I get the exact same error as before. Could you please explain me how it works, maybe I have to change something else too.
@Roman Strange. That's the only place where you are using that variable. If it's empty it shouldn't display anything and if it's not - it should render proper value. Please try to clear your cache and restart your app.
i think it should be this.state.comments.length ? ...... because initial value is [] :)
@MayankShukla Yep, I've missed the default value of that variable, thanks!
0

The problem is that the data this.state.comments[0] isn't available when you try to use it. So you need to perform a check before you use it

render() {
    return (
      <div>
        <ul>
            <li>
                {this.state.comments[0]? this.state.comments[0].a: ''}
            </li>
        </ul>
      </div>
    );
  }
}

However I think your would like to map over the comments instead of displaying it. You could do it like

render() {
    return (
      <div>
        <ul>
            <li>
                {this.state.comments && this.state.comments.map((comment) => {
                    <li>{comment.a}</li>
                })}
            </li>
        </ul>
      </div>
    );
  }
}

Comments

0

make sure you're checking comments and length too, like so -

(this.state.comments && this.state.comments.length > 0) ? 
    this.state.comments[0].a : null

Comments

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.