gpt4 book ai didi

javascript - 无法读取未定义的属性 'getState' - Redux

转载 作者:行者123 更新时间:2023-12-02 23:16:28 26 4
gpt4 key购买 nike

我有一个 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 状态(尽管我看不到任何状态)。

最佳答案

您在 store.js 中默认导出,但随后使用命名导入:

import {
store
} from './js/store/store';

更改为:

import store from './js/store/store';

关于javascript - 无法读取未定义的属性 'getState' - Redux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57144203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com