简体   繁体   中英

I am getting this error: TypeError: store.getState is not a function

I am getting the error:'TypeError: store.getState is not a function' and I can't determine where the problem is

here's how I created the store:

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));

and here's the 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")
);

and here's the thunk function:

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;

and here's a sandbox of the problem:

https://codesandbox.io/s/polished-sunset-wxefc?file=/src/index.js

here's a screenshot of the error:

在此处输入图像描述

The store is not the default export from the ./App.jsx module. You either need to add curly braces to your import or export store as the default :

// App.jsx

export const store = /* ... */

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

or

// App.jsx

export default const store = /* ... */

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM