简体   繁体   中英

Different Problem: React store.getState is not a function

Here is my code: store.js

import { createStore, applyMiddleware, compose } from "redux";

import thunk from "redux-thunk";
import rootReducer from "./reducers";

const initialState = {};

const middleware = [thunk];

const store = createStore(
  rootReducer,
  initialState,
  compose(
    applyMiddleware(...middleware),
    (window.__REDUX_DEVTOOLS_EXTENSION__ &&
    window.__REDUX_DEVTOOLS_EXTENSION__()) ||
    compose
    )
);

App.js

import React, { Component } from 'react';
import './sass/main.scss';

import { BrowserRouter as Router, Route } from "react-router-dom";

import Landing from './components/pages/Landing';
import { Provider } from "react-redux";
import store from "../src/store";

import Register from "./components/auth/Register";
import Login from "./components/auth/Login";

class App extends Component {
  render() {
    return (
      <Provider store={store}>
          <Router>
             <div className="App">
                 <Route exact path='/' component={Landing}/>
                 <Route exact path="/login" component={Login}/>
                 <Route exact path="/register" component={Register}/>
             </div>
         </Router>
     </Provider>
    );
  }
}

export default App;

I can't find where the problem is. I tried to debug it, but can't found what really make it those error. error: Uncaught TypeError: store.getState is not a function.

./src/App.js Attempted import error: '../src/store' does not contain a default export (imported as 'store').

The first thing tried is adding a export default createStore but that bring up other error message saying Line 10: 'store' is assigned a value but never used and TypeError: store.getState is not a function

In your App.js you are trying to import store from "../src/store"; . So system will try to import something from ../src/store.js but you never export any variable in that file.

You can update store.js to add export statement

const store = createStore(
  rootReducer,
  initialState,
  compose(
    applyMiddleware(...middleware),
    (window.__REDUX_DEVTOOLS_EXTENSION__ &&
    window.__REDUX_DEVTOOLS_EXTENSION__()) ||
    compose
    )
);

export default store;

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