Skip to content Skip to sidebar Skip to footer

Map Function In React (err: Typeerror: E.map Is Not A Function)

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 (

Solution 1:

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)

Solution 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.

Solution 3:

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) returnnull;
   ...
}

Solution 4:

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) =>
              <divkey={index}className="row"><spandata = {data } className="number col-4 col-md-8">{item._id}</span><spandata = {data } className="date col-4 col-md-2">{item.date}</span><spandata = {data }  className="tag col-4 col-md-2">{item.tag}</span><divclassName="col-md-12 ">
                    {item.text}
                  </div></div>
          )}
      </div>
    )
  }

Post a Comment for "Map Function In React (err: Typeerror: E.map Is Not A Function)"