我在React中使用immutable-js和react-immutable-proptypes.
// CommentBox.jsx
getInitialState() {
return {
comments: Immutable.List.of(
{author: 'Pete Hunt', text: 'Hey there!'},
{author: 'Justin Gordon', text: 'Aloha from @railsonmaui'}
)
};
},
render(){
console.log(this.state.comments.constructor);
return (
<div className='commentBox container'>
<h1>Comments</h1>
<CommentForm url={this.props.url} />
<CommentList comments={this.state.comments} />
</div>
);
}
// CommentList.jsx
propTypes: {
comments: React.PropTypes.instanceOf(Immutable.List),
},
// CommentStore.js
handleAddComment(comment) {
this.comments.push(comment);
}
Run Code Online (Sandbox Code Playgroud)
当页面初始化时,没问题,一切正常,没有警告.控制台日志显示comments是function List(value).当我添加新评论时,它看起来效果很好,但是有一个警告
警告:propType失败:无效的prop
comments提供给CommentList,预期的实例List.检查渲染方法CommentBox.
并且控制台日志显示comments是function Array().那么,为什么comments构造函数会List变为 …