我有一个React应用,现在我使用redux。该应用程序正常运行,但现在出现错误:
Uncaught TypeError: Cannot read property 'getState' of undefined
at new Provider (Provider.js:25)
at vf (react-dom.production.min.js:132)
at Og (react-dom.production.min.js:167)
at Tg (react-dom.production.min.js:180)
at bi (react-dom.production.min.js:232)
at ci (react-dom.production.min.js:233)
at Di (react-dom.production.min.js:249)
at Yh (react-dom.production.min.js:248)
at Xh (react-dom.production.min.js:245)
at qf (react-dom.production.min.js:243)
有人可以在这里帮忙吗?
代码:
store.js:
import {
combineReducers,
createStore
} from 'redux';
import campaignReducer from './campaign/reducer';
const reducer = combineReducers({
campaign: campaignReducer
});
const enhancedCreateStore = createStore(
reducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
export default enhancedCreateStore;
App.jsx:
import React from 'react';
import ReactDOM from 'react-dom';
import {
HashRouter as Router
} from 'react-router-dom';
import {
Provider
} from 'react-redux';
// tell webpack about all used svg images
/* eslint-disable */
import svgSprite from './js/services/helper';
/* eslint-enable */
import Shop from './js/components/shop';
import {
store
} from './js/store/store';
// render the {React.Component}
ReactDOM.render( <
Provider store = {
store
} >
<
Router >
<
Shop / >
<
/Router> < /
Provider > ,
document.querySelector('#order-form')
);
Shop.jsx(连接部分):
const stateMapper = ({
isOpen,
message,
showOkBtn,
showCancelBtn
}) => ({
isOpen,
message,
showOkBtn,
showCancelBtn
});
const dispatchMapper = dispatch => ({
onSetModalOpen: options => dispatch(Action.openCampaignModal(options)),
onSetModalClosed: () => dispatch(Action.closeCampaignModal())
});
export default connect(
stateMapper,
dispatchMapper
)(Shop);
helper.js:
export const createReducer = (initialState, handlers) => (state = initialState, action) => {
// eslint-disable-next-line
if (action.type in handlers) {
return handlers[action.type](state, action);
}
return state;
};
export const multiUse = (reducer, name = '') => (state = null, action) => {
if (action.name !== name) return state;
return reducer(state, action);
};
action.js:
export const OPEN_CAMPAIGN_MODAL = 'OPEN_CAMPAIGN_MODAL';
export const CLOSE_CAMPAIGN_MODAL = 'CLOSE_CAMPAIGN_MODAL';
export const SET_CAMPAIGN = 'SET_CAMPAIGN';
export const openCampaignModal = ({
message,
showOkBtn,
showCancelBtn
}) => ({
type: OPEN_CAMPAIGN_MODAL,
modal: {
isOpen: true,
message,
showOkBtn,
showCancelBtn
}
});
export const closeCampaignModal = () => ({
type: CLOSE_CAMPAIGN_MODAL,
modal: {
isOpen: false
}
});
export const setCampaign = name => ({
type: SET_CAMPAIGN,
selected: name
});
reducer.js:
import {
createReducer
} from '../helper';
import * as Action from './actions';
export default createReducer({
modal: {
isOpen: false,
message: null,
showOkBtn: true,
showCancelBtn: false
},
selected: ''
}, {
[Action.OPEN_CAMPAIGN_MODAL]: (state, {
isOpen,
message,
showOkBtn,
showCancelBtn
}) =>
Object.assign({}, state, {
modal: {
isOpen,
message,
showOkBtn,
showCancelBtn
}
}),
[Action.CLOSE_CAMPAIGN_MODAL]: (state, {
isOpen
}) =>
Object.assign({}, state, {
modal: {
isOpen
}
}),
[Action.SET_CAMPAIGN]: (state, action) =>
Object.assign({}, state, {
selected: action.selected
})
});
这是什么问题?我做了很多调试,chrome中的redux-dev-tools似乎也显示了已初始化的redux状态(尽管我看不到任何状态)。
答案 0 :(得分:1)
您在store.js中export default
,但随后使用了一个命名导入:
import {
store
} from './js/store/store';
更改为此:
import store from './js/store/store';