'group' 状态保存在 store.default.group 而不是 store.group
'group' state gets saved in store.default.group instead of store.group
我制作了一个名为 'group' 的减速器,但是当我打电话给商店时,我看到它保存在 store.default.group 中。为什么它不在 store.group 中呢?为什么我里面有默认值?
索引减速器:
import { combineReducers } from 'redux'
import counter from './counter'
import group from './groupReducer'
export default combineReducers({
counter,
group,
})
组减速器:
import * as types from '../actions/actionTypes';
const initialState = {
name: null,
route: null,
grade: null,
coins: 0,
image: null,
};
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case types.SET_GROUP_NAME:
return {
...state,
name: action.name
};
case types.SET_GROUP_ROUTE:
return {
...state,
route: action.route
};
case types.ADD_GROUP_COINS:
return {
...state,
coins: state.count + action.coins
};
case types.REMOVE_GROUP_COINS:
return {
...state,
coins: state.count - action.coins
};
case types.SET_GROUP_IMAGE:
return {
...state,
image: actions.image
};
case types.SET_GROUP_grade:
return {
...state,
grade: action.grade
};
default:
return state;
}
}
我的连接函数:
export default connect(store => ({
group: store.default.group
}),
(dispatch) => ({
actions: bindActionCreators(groupActions, dispatch)
})
)(StartGame);
编辑:
正在创建商店:
import React, {Component} from 'react';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import * as reducers from '../reducers';
import Routing from './Routing';
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const reducer = combineReducers(reducers);
const store = createStoreWithMiddleware(reducer);
export default class App extends Component {
render() {
return (
<Provider store={store}>
<Routing />
</Provider>
);
}
}
您在创建商店对象时在您的应用程序中使用了两次 combineReducers
。一个在 reducer index.js 内部,另一个在创建存储对象时。 删除一个 combineReducers 然后你的商店应该按预期工作。
我制作了一个名为 'group' 的减速器,但是当我打电话给商店时,我看到它保存在 store.default.group 中。为什么它不在 store.group 中呢?为什么我里面有默认值?
索引减速器:
import { combineReducers } from 'redux'
import counter from './counter'
import group from './groupReducer'
export default combineReducers({
counter,
group,
})
组减速器:
import * as types from '../actions/actionTypes';
const initialState = {
name: null,
route: null,
grade: null,
coins: 0,
image: null,
};
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case types.SET_GROUP_NAME:
return {
...state,
name: action.name
};
case types.SET_GROUP_ROUTE:
return {
...state,
route: action.route
};
case types.ADD_GROUP_COINS:
return {
...state,
coins: state.count + action.coins
};
case types.REMOVE_GROUP_COINS:
return {
...state,
coins: state.count - action.coins
};
case types.SET_GROUP_IMAGE:
return {
...state,
image: actions.image
};
case types.SET_GROUP_grade:
return {
...state,
grade: action.grade
};
default:
return state;
}
}
我的连接函数:
export default connect(store => ({
group: store.default.group
}),
(dispatch) => ({
actions: bindActionCreators(groupActions, dispatch)
})
)(StartGame);
编辑:
正在创建商店:
import React, {Component} from 'react';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import * as reducers from '../reducers';
import Routing from './Routing';
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const reducer = combineReducers(reducers);
const store = createStoreWithMiddleware(reducer);
export default class App extends Component {
render() {
return (
<Provider store={store}>
<Routing />
</Provider>
);
}
}
您在创建商店对象时在您的应用程序中使用了两次 combineReducers
。一个在 reducer index.js 内部,另一个在创建存储对象时。 删除一个 combineReducers 然后你的商店应该按预期工作。