简体   繁体   中英

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. So How to convert a List to Array of Objects.

My initialState

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

And I am also using SSR so my window.State will coverted to Immutable data structure

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

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:

// 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 is used to create an equivalent function of Redux combineReducers that works with Immutable.js state.

When Redux createStore reducer is created using redux-immutable then initialState must be an instance of Immutable.Collection.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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