小编rub*_*yu2的帖子

在react-js中的immutable-js时使用PropTypes

我在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)

当页面初始化时,没问题,一切正常,没有警告.控制台日志显示commentsfunction List(value).当我添加新评论时,它看起来效果很好,但是有一个警告

警告:propType失败:无效的prop comments提供给 CommentList,预期的实例List.检查渲染方法 CommentBox.

并且控制台日志显示commentsfunction Array().那么,为什么comments构造函数会List变为 …

javascript reactjs immutable.js

12
推荐指数
1
解决办法
8558
查看次数

标签 统计

immutable.js ×1

javascript ×1

reactjs ×1