簡體   English   中英

如何在Redux中不使用.toJS()將不可變js數據結構轉換為用例數組?

[英]How to covert Immutable js Data Structure to an use case array without using .toJS() in Redux?

在Redux + Immutable js的新版本中,我發現toJS方法存在一些性能問題,但是在我的用例中,我找不到任何替代方法。 那么如何將列表轉換為對象數組。

我的initialState

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

而且我也在使用SSR,所以我的window.State將覆蓋為不可變數據結構

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
})

這是我基於組件的用例

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

它並不能直接回答您的問題,但是聽起來您是在問這個問題,因為您不知道如何使Redux和ImmutableJs很好地協同工作:

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

我在項目中使用了一個名為redux-immutable的庫,該庫允許您將整個狀態作為ImmutableJS對象進行管理:

redux-immutable用於創建與Immutable.js狀態一起使用的Redux CombineReducers的等效功能。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM