我在應用程序中使用react + flux。我試圖使用不可變的js來加速渲染過程,因爲每次我對狀態做出任何小改動時,都會嘗試協調所有的DOM(這很慢)。如何在flux中正確傳遞immutablejs對象
我遇到的問題是,在我的store.js中,我可以將我的狀態轉換爲不可變的Map對象。但是,只要將該對象傳遞給應用程序,它就不再被識別爲Map對象,而只是一個普通對象。這意味着我不能使用任何設置或獲取附帶地圖對象功能
這是我到目前爲止有:
Store.js
var Immutable = require("immutable");
var Store = function(){
var jsState = { object1 : "state of object 1",
object2 : "state of object 2"}
this.globalState = Immutable.fromJS(globalState);
this._getGlobalState = function(){
//console will log: Map { size=2, _root=ArrayMapNode, __altered=false, more...}
//this.globalState.get("object1"); will work
console.log(this.globalState);
return this.globalState;
}
}
App.js
var Store = require("./Store.js");
var Map = require("immutable").Map
var App = React.createClass({
getInitialState: function(){
return ({});
},
componentWillMount: function()
this._getStateFromStore(); //will get the immutable state from the store
},
_getStateFromStore: function()
{
return this.setState(Store._getGlobalState());
},
render: function(){
//this will return Object { size=2, _root=ArrayMapNode, __altered=false, more...}
//this.state.get("object1") will NOT work
console.log(this.state);
return <div>This is in App</div>
}
});
我在這裏做錯了什麼?我是否缺少任何文件中的任何模塊?非常感謝!
這將是更快地創建'Immutable.Map'直接:'VAR jsState = Immutable.Map({object1 :「對象1的狀態」, object2:「對象2的狀態」})'而不是使用'fromJS'。此外,你會想要一個'componentShouldUpdate'方法來利用不可變的優勢。查看['PureRenderMixin'](https://facebook.github.io/react/docs/pure-render-mixin.html)是一個簡單的方法。 –