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
因此,在最后一步,状态不会更新。任何想法为什么? 谢谢!