鍍金池/ 問答/HTML/ 初學(xué)redux,報(bào)錯(cuò)Cannot read property 'map' of

初學(xué)redux,報(bào)錯(cuò)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>
                    展示不同標(biāo)簽
                </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

圖片描述

回答
編輯回答
何蘇葉

你的DifferentLabelsDiv 組件有一個(gè)todos的屬性,但是你并沒有給這個(gè)組件傳遞這個(gè)屬性,所以todosundefined,所以你調(diào)用map方法會(huì)報(bào)錯(cuò),
解決方法:

  1. 傳遞todos屬性
  2. DifferentLabelsDiv這個(gè)組件一個(gè)默認(rèn)的todos屬性。
2017年4月19日 06:36
編輯回答
舊城人
const DifferentLabelsDiv = ({ todos = [], toggleTodo }) ...

改成這樣

2017年3月12日 23:45