2017-06-29 33 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

相关问题