初学redux,报错Cannot read property 'map' of undefined

import React from 'react';
import DifferentLabels from './DifferentLabels'

const DifferentLabelsDiv = ({ todos, toggleTodo }) => (
    <div>
        {todos.map(todo =>
            <DifferentLabels
                key={todo.id}
                {...todo}
            />
        )}
    </div>
)

class DifferentLabelsList extends React.Component {

    render(){
        return (
            <div className={'left_layout'}>
                <div>
                    展示不同标签
                </div>
                <DifferentLabelsDiv />
            </div>
        )
    }
}

export default DifferentLabelsList;
import React from 'react'
import PropTypes from 'prop-types'

const DifferentLabels = ({ onClick, completed, text }) => (
    <div
    >
        {text}
    </div>
);

export default DifferentLabels

图片描述

阅读 6.8k
2 个回答
const DifferentLabelsDiv = ({ todos = [], toggleTodo }) ...

改成这样

你的DifferentLabelsDiv 组件有一个todos的属性,但是你并没有给这个组件传递这个属性,所以todosundefined,所以你调用map方法会报错,
解决方法:

  1. 传递todos属性
  2. DifferentLabelsDiv这个组件一个默认的todos属性。
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题