【问题标题】:How to add a reducer to a React Redux app如何将 reducer 添加到 React Redux 应用程序
【发布时间】:2017-10-24 20:50:05
【问题描述】:

我有以下商店:

setup.js

import catReducer from '../reducers/catReducer';

let store;

const initStore = ({onRehydrationComplete}) => {
  store = createStore(
    combineReducers({
      ...reactDeviseReducers,
      catReducer,
      form: formReducer,
      router: routerReducer,
      apollo: apolloClient.reducer()
    }),
    {},
    compose(
      applyMiddleware(
        thunk,
        routerMiddleware(history),
        apolloClient.middleware()
      ),
      autoRehydrate()
    )
  );

  persistStore(store, {
    blacklist: [
      'form'
    ]
  }, onRehydrationComplete);

  return store;
};

我正在尝试添加减速器catReducer,如上所示。当catReducer 不存在时,一切正常,当我添加catReducer 并稍后在组件中记录state 时,catReducer 未按预期显示在商店中。我做错了什么?

catReducer.js

import * as types from '../actions/actionTypes';
import initialState from './initialState';

export default function catReducer(state = initialState.cats, action) {
  switch(action.type) {
    case types.LOAD_CATS_SUCCESS:
      return action.cats
    default:
      return state;
  }
}

初始状态

export default {
  cats: [],
  hobbies: []
}

我的反应组件:CatsPage.js

import React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import CatList from './CatList';
import {loadCats} from '../../actions/catActions';

class CatsPage extends React.Component {
  componentDidMount() {
    this.props.dispatch(loadCats())
  }
  render() {
    return (
      <div>
        <h1>Cats</h1>
        <div>
          <CatList cats={this.props.cats} />
        </div>
      </div>
    );
  }
}

CatsPage.propTypes = {
  cats: PropTypes.array.isRequired
};

function mapStateToProps(state, ownProps) {

  console.log('mapStateToProps')
  console.log(state)

  return {
    cats: state.cats
    //cats: [{id:1, name: "Maru"}]
  };
}

export default connect(mapStateToProps)(CatsPage);

更新

上面的JS控制台错误:

warning.js:36 Warning: Failed prop type: The prop `cats` is marked as required in `CatsPage`, but its value is `undefined`.

Warning: Failed prop type: The prop `cats` is marked as required in `CatList`, but its value is `undefined`.

CatList.js:8 Uncaught TypeError: Cannot read property 'map' of undefined

【问题讨论】:

  • 谢谢,但上面在哪里?
  • 在 combineReducers 中,您将 key 设置为 catReducer。如果你想让它成为猫,那就让它成为猫:catReducer。
  • 当我将 combineReducers 更新为 cats: catReducer, 然后输出 console.log(state) 我看到了猫对象,但该对象有错误和异常:内部服务器错误,异常:#[:en], :formats=>[:html, :text, :js,....
  • API 有问题?查看负载猫。

标签: reactjs redux react-redux redux-thunk reducers


【解决方案1】:

在您的函数mapStateToProps 中,您尝试将猫分配给state.cats,但在您的combineReducers 函数中,您的对象看起来像 {catReducer: catReducer}。 尝试将combineReducers 函数中的catReducer 条目更改为{cats: catReducer} 之类的东西

【讨论】:

  • 您是否建议将catReducer, 更改为cats: catReducer,
  • 当我这样做时,我得到一个错误,见上面的评论......谢谢
【解决方案2】:

修改您的combineReducers 呼叫。请参阅两个代码示例的第 3 行。

正确的一个

combineReducers({ ...reactDeviseReducers, cats: catReducer, form: formReducer, router: routerReducer, apollo: apolloClient.reducer() })

Your One 将被翻译为

combineReducers({ ...reactDeviseReducers, catReducer: catReducer, form: formReducer, router: routerReducer, apollo: apolloClient.reducer() })

【讨论】:

  • 谢谢,但是当我在 mapStateToProps 中输出状态时,我看到状态对象有一个猫:对象,当我打开那个打开时,有一个猫数组。不应该有cats OBJECT >cats Array(0) 对吧?
  • 不,你正在从你的 reducer 返回一个数组,所以你会在 cat 对象中得到一个数组。
【解决方案3】:

变化

 return {
    cats: state.cats
    //cats: [{id:1, name: "Maru"}]
  };

  return {
    cats: state.catReducer.cats
    //cats: [{id:1, name: "Maru"}]
  };

可能会为你解决问题

【讨论】:

  • 将其更改为 cats: state.cats.cats 确实修复了错误,这很好,但在结构上,我们不希望猫中的猫,,,我怎样才能让它只是猫而不是猫>猫?跨度>
  • 问题是你在这里使用combine reducer,这意味着会有一个root reducer,它是多个reducer的组合,你的cats对象不在根reducer中,这里,你的root reducer 由多个 reducer 组合而成(cats 是其中之一),在平行世界中,如果你只有一个 reducer,这将是 cat reducer,你可以使用state.cats,但根 reducer 来自哪里发挥它的作用是state->rootReducer->individualReducer。希望这是有道理的。
猜你喜欢
  • 2018-07-21
  • 2016-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-27
  • 1970-01-01
  • 2017-07-12
相关资源
最近更新 更多