2016-08-22 171 views
2

我的reducer不会更新我的Redux状态树,我还没找到解决方案,所以我希望得到一些帮助。这是我的代码: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 
    } 
} 

应用

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-immutableset函数期望作为参数2个字符串:键和值。在你的代码中,你传递给它一个字符串和一个对象。

根据seamless-immutable documentation设置state.firebaseDataaction.firebaseData使用合并功能与对象为传递价值:

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

其他的问题,我与你提供的例子中看到的是,在mapStateToPros功能,您正在访问的成员firebaseReducer无处可寻的状态。你打算把它映射到state.firebaseData吗?