【问题标题】:TypeError: Object(...) is not a function; when attempting to create a redux storeTypeError: Object(...) 不是函数;尝试创建 redux 商店时
【发布时间】:2019-12-06 22:39:36
【问题描述】:

我正在尝试将 Redux 集成到我的 React 应用程序中并创建一个商店,但我遇到了一个错误:

https://imgur.com/gHaNmiY

我在我的 app.js 文件中导入 Provider 并正确导入下面的文件...

store.js



import { createStore, applyMiddleware, compose } from 'react'
import rootReducer from './reducers'
import thunk from 'redux-thunk'

const initialState = {}

const middleware = [thunk]

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


export default store


app.js


import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css'
import './App.css'
import AppNavbar from './components/AppNavbar'
import ShoppingList from './components/ShoppingList'
import { Provider } from 'react-redux'
import store from './store'

function App() {
  return (
    <Provider store={store}>
      <div className="App">
        <AppNavbar />
        <ShoppingList />
      </div>  
    </Provider>

  );
}

export default App;


reducers/index.js

import { combineReducers } from 'redux'
import itemReducer from './itemReducer'

export default combineReducers({
    item: itemReducer
})

【问题讨论】:

  • 如果有效,您会将问题标记为 ✅ 吗?

标签: reactjs redux react-redux


【解决方案1】:

您从store.js 导入的方法来自 Redux 包,而不是来自react

参考 Redux 页面上的示例。
https://redux.js.org/api/compose#example

您应该导入createStore, applyMiddleware, compose,如下所示。

import { createStore, applyMiddleware, compose } from 'redux'

【讨论】:

    【解决方案2】:

    在您的 store.js 中,您导入了 { createStore, applyMiddleware, compose } from 'react' 而不是 redux

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-12
      • 2018-10-25
      • 1970-01-01
      • 2021-12-07
      • 2017-05-29
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      相关资源
      最近更新 更多