我在尝试使用 React 学习 Redux 时遇到了一些失败的 prop 类型问题。 问题是我想创建一个带有标题和一些文本的注释,但由于 Failed 属性类型,仅会显示标题。
警告:
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
最佳答案
检查state.notes
中的内容。您可以记录它或在某处设置断点并使用浏览器的调试器。
该数组中的对象似乎没有 text
属性,或者它设置为 undefined
或 null
。您可能在应该设置该值的地方出现拼写错误。
如果您看到了标题,那么其他一切似乎都按顺序进行。
关于javascript - React Redux 失败的 prop 类型 : Required prop was not specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40204250/