2017-06-29 2 views
1

Redux + Immutable jsの新機能です。toJSメソッドのパフォーマンス上の問題があります。しかし、私の使用例では、選択肢が見つかりませんでした。リストをオブジェクトの配列に変換する方法Reduxで.toJS()を使用せずに、不変のjsデータ構造をユースケース配列に変換する方法はありますか?

マイ初期状態

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

そして、私もそう私のwindow.Stateは不変のデータ構造にcovertedます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) 

しかし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 

あなたがすることができます私のプロジェクトで私が使用Reduxの、不変と呼ばれるライブラリがありますImmutabとしてあなたの全州を管理するleJSは、オブジェクト:

Reduxの-不変はImmutable.js状態で動作しますReduxの combineReducersと同等の機能を作成するために使用されます。

redux-immutableを使用してRedux createStore reducerを作成した場合、 initialStateはImmutable.Collectionのインスタンスである必要があります。

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

関連する問題