最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React map() showing undefined - Stack Overflow

programmeradmin5浏览0评论

I have a project where I used redux-saga to make an API call with axios and return the data to the store, which then I mapStateToProps using redux and now I want to map() it and show it on my DOM, but I'm getting "undefined".

Two things keep happening:

  1. either the data doesn't get called in time and the render happens to fast so it says its undefined.
  2. i get map() is not a function or for {blog.id} -- id is undefined.

When I console.log(blogs) i see my array of blogs so I'm not sure what I'm doing wrong. Is it because blogs is an array and so I need to do some kind of for loop to go through the each item and then map it?

Here is my main chunk of code and the console log

import React, {Component} from 'react';
import {connect} from 'react-redux'
import {loadBlogs} from '../../store/actions/blogActions'

class Bloglist extends Component {
    ponentDidMount() {
        this.props.loadBlogs();
    }

    render() {
        const {blogs} = this.props
        console.log(blogs)
        return (
            <div>
                <h1>{blogs.map(blog => (
                    <span>{blog.id}</span>
                ))}</h1>
            </div>
        )
    }
}

const mapStateToProps = blogs => ({
    blogs
})
const mapDispatchToProps = dispatch => ({
    loadBlogs: () => dispatch(loadBlogs()),
})

export default connect(mapStateToProps, mapDispatchToProps)(Bloglist)

here is a console log example and an error:

Uncaught TypeError: blogs.map is not a function

this is when I just ment out the map() lines and just return a "hello", this console.log is showing me that the data is ing back

Bloglist.js:14 (3) [{…}, {…}, {…}]

Please let me know if you want any more samples or info. It's really important to get this done so any help would be appreciated! Thanks!

I have a project where I used redux-saga to make an API call with axios and return the data to the store, which then I mapStateToProps using redux and now I want to map() it and show it on my DOM, but I'm getting "undefined".

Two things keep happening:

  1. either the data doesn't get called in time and the render happens to fast so it says its undefined.
  2. i get map() is not a function or for {blog.id} -- id is undefined.

When I console.log(blogs) i see my array of blogs so I'm not sure what I'm doing wrong. Is it because blogs is an array and so I need to do some kind of for loop to go through the each item and then map it?

Here is my main chunk of code and the console log

import React, {Component} from 'react';
import {connect} from 'react-redux'
import {loadBlogs} from '../../store/actions/blogActions'

class Bloglist extends Component {
    ponentDidMount() {
        this.props.loadBlogs();
    }

    render() {
        const {blogs} = this.props
        console.log(blogs)
        return (
            <div>
                <h1>{blogs.map(blog => (
                    <span>{blog.id}</span>
                ))}</h1>
            </div>
        )
    }
}

const mapStateToProps = blogs => ({
    blogs
})
const mapDispatchToProps = dispatch => ({
    loadBlogs: () => dispatch(loadBlogs()),
})

export default connect(mapStateToProps, mapDispatchToProps)(Bloglist)

here is a console log example and an error:

Uncaught TypeError: blogs.map is not a function

this is when I just ment out the map() lines and just return a "hello", this console.log is showing me that the data is ing back

Bloglist.js:14 (3) [{…}, {…}, {…}]

Please let me know if you want any more samples or info. It's really important to get this done so any help would be appreciated! Thanks!

Share Improve this question asked Jul 22, 2020 at 2:40 josephTjosephT 8421 gold badge7 silver badges16 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

There can be 2 issues.

  1. Please cross check the type of blogs, it should be array as map method works only on array.

  2. You have to check for array's length before mapping it. As map method doesn't works on empty array.
    Try this code --

     <div>
         <h1>{blogs && blogs.length > 0 ? blogs.map(blog => (
             <span>{blog.id}</span>
         )) : null}</h1>
     </div>
    

At the beginning, your blogs is not an Array. You should to update your reducer initialState, set blocks to be an empty array as default, just like

in reducer.js

const initialState = {
  blogs: [],
};

export default (state = initialState, action) => {
  switch(action) {
    case....

    default:
      return { ...state };
  } 
}

Or, you also should check the blogs before rendering.

Array.isArray(blogs) && blogs.map(item => (
  <div>
    {// item details goes here}
  </div>
))

use ? to handle this error. mostly probably the error is ing from the saga. you have to provide the code better suggest a solution.

 <div>
                <h1>{blogs?.map(blog => (
                    <span>{blog.id}</span>
                ))}</h1>
            </div>

Try like this.

class Example extends Component {
   state:{}
   render(){
   //.....code
  }
}
发布评论

评论列表(0)

  1. 暂无评论