5

I want to render items from props, I can do it with initial state, but not with response from server. My render function :

 const { data } = this.props;
    return (
      <div >
          {data.map((item, index) =>
              <div key={index} className="row">
                  <span data = { data } className="number col-4 col-md-8">{item._id}</span>
                  <span data = { data } className="date col-4 col-md-2">{item.date}</span>
                  <span data = { data }  className="tag col-4 col-md-2">{item.tag}</span>
                  <div className="col-md-12 ">
                    {item.text}
                  </div>                
              </div>
          )}
      </div>
    )
  }

I get this mistake :

TypeError: e.map is not a function

response : Object {data: Array(12), status: 200, statusText: "OK", headers: Object, config: Object…}

2
  • Where's your call to the server? Where is e.map in your code? Commented Aug 20, 2017 at 22:30
  • Can you show how are you updating your data prop from the response? It looks like data becomes undefined, so I think you are overriding something, but I cannot tell until I see the code. Commented Aug 21, 2017 at 7:32

4 Answers 4

3

Had to change parent component change this:

  this.setState({
    data: response
  })

to

  this.setState({
    data: response.data
  })

I've tried to reach the data from the child component, but it din't work (probably because of the map function)

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

Comments

2

It looks like your response is just the raw response. If you're using fetch, this is what the promise chain should look like:

fetch(fromMySource).then(resp => resp.json()).then(data => doSomething(data));

It looks like you might be trying to use resp directly which would make your data array look like the response object you posted in your question.

Comments

1

Just add these words to your map function, Because you need check if the array was existed then execute map function

const { data } = this.props;
    return (
      <div >
          {data && data.length && data.map((item, index) =>
              <div key={index} className="row">
                  <span data = { data } className="number col-4 col-md-8">{item._id}</span>
                  <span data = { data } className="date col-4 col-md-2">{item.date}</span>
                  <span data = { data }  className="tag col-4 col-md-2">{item.tag}</span>
                  <div className="col-md-12 ">
                    {item.text}
                  </div>                
              </div>
          )}
      </div>
    )
  }

1 Comment

looks like it renders while response doesn't exist yet, and doesn't rerenders after..
0

You can use default value of data, when response is pending. In this time data is not defined

const { data = [] } = this.props;

Or use condition in  jsx:

render() {
   if (!data || !data.length) return 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.