在过去的几周里,我一直在使用 facebooks 框架 React.js 和 Backbone,但我仍然不完全确定当 Backbone 集合发生变化时重新渲染 React 组件的最合适方法是什么已作为props传入。
目前我所做的是在componenentWillMount集合上设置change/add/remove侦听器并在触发时设置状态:
componentWillMount: function(){
    var myCollection = this.props.myCollection;
    var updateState = function(){
        this.setState({myCollection: myCollection.models});
    }
    myCollections.on("add remove", updateState, this);
    updateState();
}
render: function(){
    var listItems = this.state.myCollection.map(function(item){
        return <li>{item.get("someAttr")}</li>;
    });
    return <ul>{listItems}</ul>;
}
我见过将模型克隆到状态的示例:
var updateState = function () {
    this.setState({ myCollection: _.clone(this.myCollection.models) });
};
我还看到了在 props 中直接使用模型/集合而不是使用状态的变体,然后在集合/模型更改时调用 forceUpdate,导致组件重新渲染
componentWillMount: function(){
    var myCollection = this.props.myCollection;
    myCollections.on("add remove", this.forceUpdate, this);
}
render: function(){
    var listItems = this.props.myCollection.map(function(item){
        return <li>{item.get("someAttr")}</li>;
    });
    return <ul>{listItems}</ul>;
}
不同的方法有什么优点和缺点?有没有办法做到这一点,那就是React 的方式?