简体   繁体   English

如何在Redux中不使用.toJS()将不可变js数据结构转换为用例数组?

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

In am new to Redux+Immutable js I found Some performance issue with toJS method But In my use case I could not find any alternatives. 在Redux + Immutable js的新版本中,我发现toJS方法存在一些性能问题,但是在我的用例中,我找不到任何替代方法。 So How to convert a List to Array of Objects. 那么如何将列表转换为对象数组。

My initialState 我的initialState

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

And I am also using SSR so my window.State will coverted to Immutable data structure 而且我也在使用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]);
    });

Configure Store 配置商店

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

import allPosts from './allPosts'


export default combineReducers({
    allPosts,

    //ForServerSide
    routing: routerReducer
})

This is my Component Based Use Case 这是我基于组件的用例

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

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

@connect(mapStateToProps,mapDispatchToProps)

But this.props.post Still Looking Like Immutable Structure But How to use it sorry for Poor console.log presentation But I wanna to show 但是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

It doesn't directly answer your question, but it sounds like you're asking this question because you're unaware of how to make Redux and ImmutableJs play nicely together: 它并不能直接回答您的问题,但是听起来您是在问这个问题,因为您不知道如何使Redux和ImmutableJs很好地协同工作:

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

There is a library called redux-immutable which I use on my projects which allows you to manage your entire state as an ImmutableJS object: 我在项目中使用了一个名为redux-immutable的库,该库允许您将整个状态作为ImmutableJS对象进行管理:

redux-immutable is used to create an equivalent function of Redux combineReducers that works with Immutable.js state. redux-immutable用于创建与Immutable.js状态一起使用的Redux CombineReducers的等效功能。

When Redux createStore reducer is created using redux-immutable then initialState must be an instance of Immutable.Collection. 当使用redux-immutable创建Redux createStore reducer时,initialState必须是Immutable.Collection的实例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM