这是我的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请让我知道我做错了什么。
发布于 2017-05-22 08:20:45
你是在进口
import thunk from 'react-thunk';但雷击是来自redux-thunk模块。
所以你应该进口
import thunk from 'redux-thunk';此外,我认为你的电话"createStore“会有问题。
createStore采用还原器(或组合减速器)和可能的中间件。
还原器接受一个状态和一个动作,并且必须返回存储的新状态。
function reducer(state, action){
// Switch-Case for action.type
// Copy the current state and apply changes
return state;
}发布于 2017-05-22 07:45:11
从错误来看你的问题很明显。不能将函数作为类发送。在createStore()中,第一个参数是一个函数。这应该是你所拥有的综合减速器。
使用您所拥有的还原器创建一个文件,并将其导入索引文件。然后做一些类似的事情
const store = createStore(rootReducer, applyMiddleware(thunk))发布于 2019-05-08 20:51:44
这就是我所做的,我希望它能帮助到一些人
在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 projectReducerhttps://stackoverflow.com/questions/44106737
复制相似问题