【问题标题】:Why would individual reducers work, but implementing combine reducer throw errors?为什么单个减速器会工作,但实施组合减速器会抛出错误?
【发布时间】:2020-08-16 15:05:52
【问题描述】:

我正在尝试实现一个联合减速器,如果有人可以帮忙,我看到的一切都是正确的。如果我将每个单独的减速器放入其中,然后放入 rootReducer 并且没有任何作用。给我 props.mutts/felines/applicants.map 的错误不是函数。

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import { createStore, applyMiddleware, compose} from 'redux';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import { combineReducers } from 'redux';
import  applicantReducer  from './reducers/applicantReducer';
import  catReducer  from './reducers/catReducer';
import  dogReducer  from './reducers/dogReducer';
import App from './App';

import * as serviceWorker from './serviceWorker';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const rootReducer = combineReducers({
       applicants: applicantReducer, 
       cats: catReducer,
       dogs: dogReducer
})

let store = createStore(rootReducer, composeEnhancers(applyMiddleware(thunk)))

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

【问题讨论】:

    标签: reactjs react-redux redux-thunk


    【解决方案1】:

    函数ma​​p只能用于数组,不能用于对象。

    如果你 console.log() props.mutts ,你会看到它是一个 OBJECT。

    【讨论】:

      【解决方案2】:

      我需要在容器文件中的 mapStateToProps 中添加减速器。

      const mapStateToProps = (state) => {
          return{
              cats: state.catReducer.cats
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-26
        • 2014-09-20
        • 1970-01-01
        • 2019-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多