gpt4 book ai didi

react-redux - TypeError : store. getState 不是 react-redux 中的函数

转载 作者:行者123 更新时间:2023-12-03 18:37:42 25 4
gpt4 key购买 nike

我按照说明从这个库创建了商店

https://github.com/jesperorb/redux-cart

源代码/商店/index.js:
import { createStore } from 'redux';
//Import our rootreducer from '../reducers/index.js' this hold our whole state
import rootReducer from '../reducers/index.js';

/**
* @param {Object} initialState
*/
export default function store() {
return createStore(
rootReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
}
```

src/actions/cart-actions.js:
/** We don't have to use the variable 'payload', we can also just name it: 'item'
* @param {Object} item
*/
export function addToCart(item) {
return {
type: 'ADD',
item: item
};
}

export function removeFromCart(item) {
return {
type: 'REMOVE',
item: item
};
}

源代码/索引.js:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import registerServiceWorker from './registerServiceWorker';
import store from './store/index.js';


ReactDOM.render(
<Provider store={store}>
<App />
</Provider>, document.getElementById('root'));
registerServiceWorker();

但我收到错误 TypeError: store.getState is not a function 。有人可以建议我应该首先研究哪些步骤吗?

最佳答案

看起来你忘记在 store 中创建一个 index.js 对象

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import registerServiceWorker from './registerServiceWorker';
import Store from './store/index.js';

// forgot to create store obj
const store = Store();

ReactDOM.render(
<Provider store={store}>
<App />
</Provider>, document.getElementById('root'));
registerServiceWorker();

关于react-redux - TypeError : store. getState 不是 react-redux 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49561697/

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