在应用 applyMiddleware(thunk) 获取 "Cannot call a class as a function" 时,在 React js 中

while applying applyMiddleware(thunk) getting "Cannot call a class as a function" , in react js

这是我的 index.js 文件代码 -

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
import { BrowserRouter as Router, Route } from 'react-router-dom'; 
import { Provider } from 'react-redux';
import thunk from 'react-thunk';
import { createStore, applyMiddleware } from 'redux';
import Login from './Components/LoginRegister';

const store= createStore(
        (state = {}) => state, 
        applyMiddleware(thunk)
    );

ReactDOM.render(( 
    <Provider store={store}>
      <Router>
        <switch>
            <Route exact path="/" component={App} />
            <Route path="/Loginregister" component={Login} />
        </switch>
      </Router>
    </Provider>  ),
  document.getElementById('root')
);

因为我在 'applyMiddleware' 中将 'thunk' 作为 'applyMiddleware(thunk)' 传递,然后我在控制台上遇到以下错误 -

Uncaught TypeError: Cannot call a class as a function
    at _classCallCheck (index.js:15)
    at ReactThunk (index.js:36)
    at applyMiddleware.js:51
    at createStore (createStore.js:65)
    at Object.<anonymous> (index.js:11)
    at __webpack_require__ (bootstrap b42575b…:555)
    at fn (bootstrap b42575b…:86)
    at Object.<anonymous> (bootstrap b42575b…:578)
    at __webpack_require__ (bootstrap b42575b…:555)
    at bootstrap b42575b…:578

请让我知道我做错了什么。

好吧,从错误中可以看出你的问题。您不能将函数作为 class 发送。在 createStore() 你的第一个参数是一个函数。它应该是你拥有的组合减速器。

使用您拥有的减速器创建一个文件,将其导入索引文件。然后做类似

const store = createStore(rootReducer, applyMiddleware(thunk))

您正在导入

import thunk from 'react-thunk';

但是 thunk 来自 redux-thunk 模块。

所以你应该导入

import thunk from 'redux-thunk';

另外我觉得你调用"createStore"会有问题。

createStore 采用减速器(或组合减速器)和可能的中间件。

reducer 接受一个状态和一个动作,并且必须 return 存储的新状态。

function reducer(state, action){

  // Switch-Case for action.type
  // Copy the current state and apply changes

  return state;
}

这是我做的,希望对大家有所帮助

index.js

import { Provider } from 'react-redux'
import thunk from 'redux-thunk'

const store = createStore(rootReducer, applyMiddleware(thunk))

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

.store/reducers/rootReducer.js

import authReducer from './authReducer'
import projectReducer from './projectReducer'
import { combineReducers } from 'redux'

const rootReducer = combineReducers({
  auth: authReducer,
  project: projectReducer,
})

export default rootReducer

并在 .store/reducers/projectReducer.js

const initState = {
  projects: [
    { id: '1', title: 'help me find peach', content: 'blah blah blah' },
    { id: '2', title: 'collect all the stars', content: 'blah blah blah' },
    { id: '3', title: 'egg hunt with yoshi', content: 'blah blah blah' },
  ],
}

const projectReducer = (state = initState, action) => {
  switch (action.type) {
    case 'CREATE_PROJECT':
      console.log('create project', action.project)
      return state
    default:
      return state
  }
}

export default projectReducer