同时应用applyMiddleware(thunk)获取“无法将类作为函数调用”,在react js中

时间:2017-05-22 07:14:56

标签: reactjs redux-thunk

这是我的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

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

3 个答案:

答案 0 :(得分:15)

您正在导入

import thunk from 'react-thunk';

但是thunk来自redux-thunk模块。

所以你应该导入

import thunk from 'redux-thunk';

此外,我认为你打电话给&#34; createStore&#34;会有问题。

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

reducer采用状态和操作,并且必须返回商店的新状态。

function reducer(state, action){

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

  return state;
}

答案 1 :(得分:1)

嗯,从错误中你的问题很明显。您不能将函数作为类发送。在createStore()中你的第一个参数是一个函数。它应该是你拥有的减速器。

使用您拥有的reducer创建一个文件,将其导入索引文件。然后做一些像

这样的事情
const store = createStore(rootReducer, applyMiddleware(thunk))

答案 2 :(得分:1)

这就是我所做的,我希望它可以对某人有所帮助

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