2016-08-22 56 views
2

Reduce状態ツリーが更新されず、解決策が見つかりませんでしたので、私はいくつかの助けを得ることを望んでいました。これは私のコードです:Redux状態が更新されない

リデューサー

import Immutable from 'seamless-immutable' 
import { loadFirebaseData } from '../actions/firebaseActions' 

const FETCH_FIREBASE_DATA = 'FETCH_FIREBASE_DATA' 

const initialState = Immutable({}) 

export default function firebaseReducer(state = initialState, action) { 
    switch (action.type) { 
    case FETCH_FIREBASE_DATA: 
    return state 
    .set('firebaseData', fetchData(action)) 
    } 
    return state 
} 


function fetchData(action) { 
    return { 
    firebaseData: action.firebaseData 
    } 
} 

のApp

class App extends Component { 

    componentWillMount() { 
    this.props.fetchFirebaseData('gallery') 
    } 

function mapStateToProps(state) { 
    console.log('state',state.firebaseReducer) 
    return { 
    galleryItems: state.firebaseReducer 
    } 
} 

function mapDispatchToProps(dispatch) { 
    return bindActionCreators(firebaseActions, dispatch) 
} 

export default connect(mapStateToProps, mapDispatchToProps)(App) 

そして、私は減速の実行をログアウトした場合:

import Immutable from 'seamless-immutable' 
import { loadFirebaseData } from '../actions/firebaseActions' 

const FETCH_FIREBASE_DATA = 'FETCH_FIREBASE_DATA' 

const initialState = Immutable({}) 

export default function firebaseReducer(state = initialState, action) { 

    console.log(1) 
    switch (action.type) { 

    case FETCH_FIREBASE_DATA: 
    console.log(2) 
    return state 
    .set('firebaseData', fetchData(action)) 
    } 

    console.log(3) 
    console.log('state', state) 
    return state 
} 


function fetchData(action) { 
    return { 
    firebaseData: action.firebaseData 
    } 
} 

コンソールには:

1 
3 
state: Object {__immutable_invariants_hold: true} 
1 
3 
Object {__immutable_invariants_hold: true} 
Object {__immutable_invariants_hold: true} 
1 
2 

したがって、最後の手順で状態が更新されません。なぜどんな考え? ありがとう!

答えて

0

あなたは間違った機能を使って店舗を変更していると思います。 seamless-immutableを使用しているので、set関数はパラメータとして2つの文字列を要求します:キーと値。あなたのコードでは、文字列とオブジェクトを渡します。

としてオブジェクトとaction.firebaseData使用のマージ機能によって渡された値にstate.firebaseDataを設定するseamless-immutable documentationによると:私はあなたの提供例を参照の他の問題はmapStateToPros機能で自分がメンバーfirebaseReducerにアクセスしているということである

export default function firebaseReducer(state = initialState, action) { 
    switch (action.type) { 
    case FETCH_FIREBASE_DATA: 
     return state 
     .merge(fetchData(action)) 
    } 
    return state 
} 

それはどこにも見つからない状態です。 state.firebaseDataにマップしますか?

関連する問題