[英]State undefined in new Reducer
I'm trying to create a new reducer, following the format of other code in the current build, and state is coming up as undefined within the new reducer. 我正在尝试按照当前构建中其他代码的格式创建新的reducer,并且在新的reducer中状态变为未定义。
When I try to console.log(this.props), the state that I am trying to pass in does not appear (ie nothing referring to choroplethData). 当我尝试console.log(this.props)时,我尝试传递的状态没有出现(即,没有内容指向choroplethData)。
// the new reducer file reducers/chart.js
import * as CONSTANTS from '../constants/charts';
const initialState = {
choroplethData: {
data: {},
error: '',
loading: false,
},
} // end of initialState
export default (state = initialState, action => {
switch(action.type) {
case CONSTANTS.GET_CHOROPLETH_DATA_REQUEST:
return {
...state,
choroplethData: {
...state.choroplethData,
loading: true,
},
}
case CONSTANTS.GET_CHOROPLETH_DATA_SUCCESS:
return {
...state,
choroplethData: {
...state.choroplethData,
data: action,
loading: false,
},
}
case CONSTANTS.GET_CHOROPLETH_DATA_ERROR:
return {
...state,
choroplethData: {
...state.choroplethData,
error: action.error,
},
}
default:
return state
} // end of switch(action.type)
}) // end of export default (state = initialState, action)
// reducers/index.js
import { combineReducers } from 'redux';
...
import chartsReducer from './charts';
export default combineReducers({
...
charts: chartsReducer,
});
// Charts/index.js
import { connect } from 'react-redux';
import Choropleth from './Choropleth';
import './Choropleth.css';
import {
getChoroplethDataRequest,
} from '../../redux/actions/charts';
const mapStateToProps = state => ({
choroplethData: state.choroplethData,
})
const mapDispatchToProps = {
getChoroplethDataRequest,
}
export default connect(
mapStateToProps,
mapDispatchToProps,
)(Choropleth)
// configureStore.js
import { createStore, applyMiddleware } from 'redux';
import { routerMiddleware } from 'react-router-redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import createHistory from 'history/createBrowserHistory';
import createSagaMiddleware from 'redux-saga';
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';
import rootReducer from './redux/reducers';
import sagas from './redux/sagas';
const persistConfig = {
key: 'root',
storage,
};
const persistedReducer = persistReducer(persistConfig, rootReducer);
export const history = createHistory();
const devToolsOptions = {
trace: true,
};
const composeEnhancers = composeWithDevTools(devToolsOptions);
const sagaMiddleware = createSagaMiddleware();
const middleware = [routerMiddleware(history), sagaMiddleware];
const store = createStore(
persistedReducer,
composeEnhancers(applyMiddleware(...middleware)),
);
const persistor = persistStore(store);
sagaMiddleware.run(sagas);
export default { store, persistor };
My goal is to have a class component called Choropleth that I can then send to one or more other class components. 我的目标是拥有一个叫做Choropleth的类组件,然后可以将其发送给一个或多个其他类组件。 It would be nice if I could isolate all of the choropleth (and other charts) functionality to their specific class components, including their actions/watchers/reducers.
如果能将所有的choropleth(和其他图表)功能隔离到其特定的类组件(包括其动作/观察者/还原器),那将是很好的。
in Charts/index.js, use state.charts.choroplethData
instead of state.choroplethData
. 在Charts / index.js中,使用
state.charts.choroplethData
而不是state.choroplethData
。 As you initial defined that state as charts
in reducers/index.js 在您最初将状态定义为reducers / index.js中的
charts
时
In Carts/index.js
file you should change the mapStateToProps
function 在
Carts/index.js
文件中,您应该更改mapStateToProps
函数
const mapStateToProps = state => ({
choroplethData: state.charts.choroplethData,
})
``
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.