我收到错误:'TypeError: store.getState is not a function',我无法确定问题出在哪里
这是我创建商店的方式:
import { applyMiddleware, createStore } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './rootReducer';
const initialState = {
pending: false,
products: [],
error: null
}
const middlewares = [thunk];
export const store=createStore(rootReducer, initialState, applyMiddleware(...middlewares));
这里是 index.js:
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import store from './App'
import App from "./App";
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
这里是 thunk 函数:
function fetchProducts() {
return dispatch => {
dispatch(fetchProductsPending());
fetch('https://api.spacexdata.com/v3/launches')
.then(res => res.json()
)
.then(
res => {
if(res.error) {
throw(res.error);
}
dispatch(fetchProductsSuccess(res.products));
return res.products;
})
.catch(error => {
dispatch(fetchProductsError(error));
})
}
}
export default fetchProducts;
这是问题的沙箱:
https://codesandbox.io/s/polished-sunset-wxefc?file=/src/index.js
这是错误的屏幕截图:
答案 0 :(得分:1)
store
不是 default
模块的 ./App.jsx
导出。您需要在导入或导出 store
中添加花括号作为 default
:
// App.jsx
export const store = /* ... */
// index.js
import { store } from './App';
或
// App.jsx
export default const store = /* ... */
// index.js
import store from './App';