2017-06-29 28 views
1

在新的Redux + Immutable js中,我發現了toJS方法的一些性能問題但是在我的用例中我找不到任何替代方法。所以如何將列表轉換爲對象數組。如何在不使用.toJS()的情況下將不可變的js數據結構隱藏到用例數組中?

我的初始化狀態

const initialState = fromJS({ 
    postArr:[] 
}); 

而且我也使用SSR所以我window.State將coverted到不可變的數據結構

const initialState = window.__STATE__; 

// Transform into Immutable.js collections, 
// but leave top level keys untouched for Redux 
Object 
    .keys(initialState) 
    .forEach(key => { 
     initialState[key] = fromJS(initialState[key]); 
    }); 

配置存儲

import {combineReducers} from 'redux'; 
import {routerReducer} from 'react-router-redux'; 

import allPosts from './allPosts' 


export default combineReducers({ 
    allPosts, 

    //ForServerSide 
    routing: routerReducer 
}) 

這是我的組件Based Use Case

const mapStateToProps = (state) => { 
    return{ 
     posts:state.allPosts.get('postArr') 
    } 
}; 

const mapDispatchToProps = (dispatch) => { 
    return { 
     getAllPosts:() => dispatch(allPosts.getAllPosts()) 
    } 
}; 

@connect(mapStateToProps,mapDispatchToProps) 

但this.props.post仍然在尋找像不可改變的結構,但如何使用它對不起可憐的console.log陳述,但我想,以顯示

List {size: 100, _origin: 0, _capacity: 100, _level: 5, _root: VNode…} 
size 
: 
100 
__altered 
: 
true 
__hash 
: 
undefined 
__ownerID 
: 
undefined 
_capacity 
: 
100 
_level 
: 
5 
_origin 
: 
0 
_root 
: 
VNode 
_tail 
: 
VNode 
__proto__ 
: 
IndexedIterable 

回答

0

它不直接回答你的問題,但這聽起來像你問這個問題,因爲你不知道如何使終極版和ImmutableJs起到很好的在一起:

// Transform into Immutable.js collections, 
// but leave top level keys untouched for Redux 

有一個叫終極版,不可變的,我在我的項目中使用的庫,它允許你管理你的整個國家作爲Immutab leJS對象:

redux-immutable用於創建Redux的等效函數 與Immutable.js狀態一起工作的combineReducers。

當Redux createStore reducer是使用redux-immutable創建的,然後 initialState必須是Immutable.Collection的一個實例。

https://www.npmjs.com/package/redux-immutable

相關問題