gpt4 book ai didi

reactjs - 在react js中应用applyMiddleware(thunk)时获取 "Cannot call a class as a function"

转载 作者:行者123 更新时间:2023-12-03 13:39:27 26 4
gpt4 key购买 nike

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

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

最佳答案

您正在导入

import thunk from 'react-thunk';

但是 thunk 来自 redux-thunk 模块。

所以你应该导入

import thunk from 'redux-thunk';

此外,我认为您对“createStore”的调用会有问题。

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

reducer 接受一个状态和一个操作,并且必须返回存储的新状态。

function reducer(state, action){

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

return state;
}

关于reactjs - 在react js中应用applyMiddleware(thunk)时获取 "Cannot call a class as a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44106737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com