React Redux Failed prop type:未指定必需的prop

时间:2016-10-23 14:49:01

标签: javascript reactjs redux react-redux

我遇到了一些失败的道具类型的问题,试图通过反应来学习Redux。 问题是我想创建一个带有标题和一些文本的注释,但由于Failed prop类型,只会显示标题。

警告:

warning.js:36 Warning: Failed prop type: Required prop `notes[0].text` was not specified in `NoteList`.
in NoteList (created by Connect(NoteList))
in Connect(NoteList) (created by App)
in div (created by App)
in App
in Provider

warning.js:36 Warning: Failed prop type: Required prop `text` was not specified in `Note`.
in Note (created by NoteList)
in NoteList (created by Connect(NoteList))
in Connect(NoteList) (created by App)
in div (created by App)
in App
in Provider


ListNotes.js:

import { connect } from 'react-redux'
import NotesList from '../components/NotesList'

const mapStateToProps = (state) => {
    return {
        notes: state.notes
    }
}

const ListNotes = connect(
    mapStateToProps
)(NotesList)

export default ListNotes


NotesList.js:

import React, { PropTypes } from 'react'
import Note from './Note'

const NoteList = ({ notes }) => {
    return (
        <ul>
            {notes.map(note =>
                 <note
                     key={note.id}
                     {...note}
                 />
            )}
        </ul>
    )
}


NoteList.propTypes = {
    notes: PropTypes.arrayOf(PropTypes.shape({
        id: PropTypes.number.isRequired,
        title: PropTypes.string.isRequired,
        text: PropTypes.string.isRequired
    }).isRequired).isRequired
}

export default NoteList


Note.js:

import React, { PropTypes } from 'react'

const Note = ({title, text}) => {
    return (
        <li>
            <h1>{title}</h1>
            <p>{text}</p>
        </li>
    )
}

Note.propTypes = {
    title: PropTypes.string.isRequired,
    text: PropTypes.string.isRequired
}

export default Note

1 个答案:

答案 0 :(得分:1)

检查state.notes中的内容。您可以在某处记录它或设置断点并使用浏览器的调试器。

该数组中的对象似乎没有text属性,或者设置为undefinednull。您可能有一个拼写错误,应该设置该值。

如果你看到了标题,那么其他一切似乎都是有序的。