简体   繁体   English

状态改变后调用方法

[英]Calling a method after state change

I am trying Redux in a project. 我在一个项目中尝试Redux。 I have a list of countries, all of which have a price, and I want to select the countries with prices between minPrice and maxPrice values. 我有一个国家列表,所有这些都有价格,我想选择价格介于minPricemaxPrice值之间的国家/ maxPrice I am doing that in this method: 我在这个方法中这样做:

  filterPrice: function(minPrice, maxPrice) {
    var newCountries = [];
    _.forEach(this.props.countries, function(country) {
        var price = country["price"];
        if(minPrice <= price && price <= maxPrice) {
            newCountries.push(country);
        }
    });
    this.setState({countries: newCountries});
  }

Now, I would like this method to be called somehow by the Redux architecture. 现在,我想通过Redux架构以某种方式调用此方法。

I have this reducer: 我有这个减速机:

const initialState = {
  minPrice: 0,
  maxPrice: Constants.PRICE_RANGE
};


export default function PriceReducer(state = initialState, action) {
  switch (action.type) {

    case types.UPDATE_MAX:
      return {
        maxPrice: action.maxPrice
      }
    case types.UPDATE_MIN:
      return {
        minPrice: action.minPrice
      }

    default:
      return state;
  }
}

And these actions: 而这些行动:

export const updateMax = (maxPrice) => {
  return {
    type: types.UPDATE_MAX,
    maxPrice: maxPrice
  };
}

export const updateMin = (minPrice) => {
  return {
    type: types.UPDATE_MIN,
    minPrice: minPrice
  };
}

I am creating a store like this: 我正在创建这样的商店:

const store = createStore(PriceReducer);

And I am calling it like this: 而我这样称呼它:

store.dispatch({type: types.UPDATE_MIN, minPrice: 10});

So how can I call filterPrice ? 那我怎么能调用filterPrice呢? Or if I can't call it, how do I change the state? 或者,如果我无法调用它,我该如何更改状态?

If you would like to use redux in the common way, you need to keep all you state in redux, to make it a single source of truth. 如果你想以常用的方式使用redux,你需要将所有的状态保存在redux中,以使其成为单一的事实来源。 So, the first thing you should do is to put you country list in redux. 所以,你应该做的第一件事就是把你的国家列表放在redux中。 Then, if you need some calculated properties, like, countries filtered by price, it's time to make selectors, using reselect library. 然后,如果您需要一些计算属性,例如按价格过滤的国家/地区,则需要使用重新选择库来制作选择器。 Also, it's a good idea to read corresponding section of redux documentation. 另外,阅读redux文档的相应部分是个好主意。

In the end, you architecture will look like this: 最后,您的架构将如下所示:

  • redux keeps minPrice , maxPrice and countries redux保留minPricemaxPricecountries
  • your container component use selectors to retrive data from state, calculate derived data and pass it to presentational components 容器组件使用选择器从状态中检索数据,计算派生数据并将其传递给表示组件

Redux should be the source of state in the component. Redux应该是组件中的状态源。 You should be able to filter the list and changes to minPrice and maxPrice should trigger your rerender. 您应该能够过滤列表并更改为minPrice,maxPrice应该触发您的重新呈现。

filteredByPrice: function() {
   return this.props.countries.filter(function(country){
      return this.props.minPrice <= this.props.country.price && 
        this.props.country.price <= this.props.maxPrice
   }
}
... 
render: function(){
  return ....
      {this.filteredByPrice().map(function(country){
        return <div> {country.name} </div>
      })} 
  ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM