Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不使用Redux中的.toJS()的情况下将不可变的js数据结构转换为用例数组?_Javascript_Reactjs_Redux_React Redux_Immutable.js - Fatal编程技术网

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

Javascript 如何在不使用Redux中的.toJS()的情况下将不可变的js数据结构转换为用例数组?,javascript,reactjs,redux,react-redux,immutable.js,Javascript,Reactjs,Redux,React Redux,Immutable.js,在我刚接触Redux+Immutable js时,我发现toJS方法存在一些性能问题,但在我的用例中,我找不到任何替代方法。因此,如何将列表转换为对象数组 我的初始状态 const initialState = fromJS({ postArr:[] }); 我也在使用SSR,所以我的窗口状态将转换为不变的数据结构 const initialState = window.__STATE__; // Transform into Immutable.js collections, //

在我刚接触Redux+Immutable js时,我发现toJS方法存在一些性能问题,但在我的用例中,我找不到任何替代方法。因此,如何将列表转换为对象数组

我的初始状态

const initialState = fromJS({
   postArr:[]
});
我也在使用SSR,所以我的窗口状态将转换为不变的数据结构

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)
但是这个.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用于创建redux的等效函数 与Immutable.js状态一起工作的CombineReducer

当使用Redux immutable创建Redux createStore reducer时 initialState必须是Immutable.Collection的实例


它并没有直接回答您的问题,但听起来您问这个问题是因为您不知道如何让Redux和ImmutableJs很好地协同工作:

// Transform into Immutable.js collections,
// but leave top level keys untouched for Redux
我在项目中使用了一个名为redux immutable的库,它允许您将整个状态作为ImmutableJS对象进行管理:

redux immutable用于创建redux的等效函数 与Immutable.js状态一起工作的CombineReducer

当使用Redux immutable创建Redux createStore reducer时 initialState必须是Immutable.Collection的实例