调度动作时,状态不会在我的减速器中发生变化

时间:2017-02-16 22:30:52

标签: redux react-redux

我无法检索缩减器中的state

MyComponent看起来像这样

const MyComponent = ({name, features, onClick}) => {
  return (
        <div>
            Hello! {name}
            <Button onClick={() => { onClick(features); }}> Weight</Button>
        </div>
    );

const mapDispatchToProps = (dispatch: any) => {
  return {
    onClick: (features) => {
        dispatch(weightSort(features));
    }
  };
};
const mapStateToProps = (state: any, ownProps: any) => {
  console.log(state); //Displays the state
  return {
    name: "John Doe",
    features: ownProps.features,
  };
};

export const FeatureBlock = connect(mapStateToProps, mapDispatchToProps)(MyComponent);

我的行动和减少者如下所示:

// Action Creator
export const  weightSort = (features) => {
console.log("inside the weight sort action creator!!!");
  return {
    type: "SET_WEIGHT_FILTER",
    filter: "DESC",
    features,
  };
};

// Reducer
export const weightFilter = (state = [], action) => {
  switch (action.type) {
    case "SET_WEIGHT_FILTER":
        console.log(state); // Gives me empty state
        console.log("++inside weight filter+++++", action); //Displays action
        return state;
    default:
        return state;
  }
};


export const FeatureBlock = connect(
  mapStateToProps,
  mapDispatchToProps,
)(MyComponent);

我在这里缺少什么?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

在你的reducer中,当你console.log(state)时,返回一个空数组是正确的,因为你还没有做任何修改它。

// Reducer
   export const weightFilter = (state = [1,2,3], action) => {
     switch (action.type) {
       case "SET_WEIGHT_FILTER":
           console.log(state); // This will show [1,2,3] because [1,2,3] is the initial state.
           console.log("++inside weight filter+++++", action); //Displays action
           return state;
       default:
           return state;
     }
   };

我的猜测是你想要减速器需要这样的东西:

// Action Creator
export const  weightSort = (name, features) => {
console.log("inside the weight sort action creator!!!");
  return {
    type: "SET_WEIGHT_FILTER",
    name,
    features,
  };
};
// Reducer
  export const weightFilter = (
    state = {
      name: '',
      features: [],
    },
    action
  ) => {
     switch (action.type) {
       case "SET_WEIGHT_FILTER":
           return {...state, name: action.name, features: action.features}
       default:
           return state;
     }
   };

然后在你的mapStateToProps中,你会映射出如下属性:

const mapStateToProps = (state: any, ownProps: any) => {
  console.log(state); //Displays the state
  return {
    name: state.weightFilter.name,
    features: state.weightFilter.features,
  };
};

并且你的按钮会有一个名称prop传递给函数,如下所示:

<Button onClick={() => { onClick(name, features); }}> Weight</Button>

如果您想对数据进行排序,可以在reducer中或容器内部进行排序。我更喜欢在容器中执行它,并喜欢使用lodash sortBy函数。它的工作原理如下:

import { sortBy } from 'lodash' //be sure to npm install lodash if you use this utility
...
...
function mapStateToProps(state) {
  return {
    name: state.weightFilter.name,
    features: sortBy(features, ['nameOfPropertyToSortBy'])
  };
}

以下是有关sortBy的{lodash文档:https://lodash.com/docs/4.17.4#sortBy

希望有所帮助!