调度动作后不显示道具

时间:2018-05-16 13:21:38

标签: javascript reactjs redux react-redux

我创建了两个根据价格对产品进行分类的操作,我将已排序的数组分配给redux state products数组,但是当我触发操作时,产品会从组件中消失..

我了解了componentWillReceivePropscomponentWillUpdate,但无法弄清楚如何使用它。

以下是行动:



// actions
    export const sortProductsByLowestPrice = () => (dispatch, getState) => {
          let products = getState().products.products.products;
          const isArray = Array.isArray(products);
          const sorted = products.slice().sort((a,b) => { return a.price > b.price})
          dispatch({
            type: SORT_PRODUCTS_BY_LOWEST_PRICE,
            payload: sorted
          })
          
    }


    export const sortProductsByHighestPrice = () => (dispatch, getState) => {
          let products = getState().products.products.products;
          const isArray = Array.isArray(products);
          const sorted = products.slice().sort((a,b) => { return a.price < b.price})
          dispatch({
            type: SORT_PRODUCTS_BY_HIGHEST_PRICE,
            payload: sorted
          })
    }

// rendering products 
  

  {this.props.products ? this.props.products.slice(0,5).map((product) => {
           return <ProductItem key={product._id} product={...product} />
              }) : null
    
    // click events
    <Button onClick={this.props.sortProductsByLowestPrice}>Lowest price</Button>
    <Button onClick={this.props.sortProductsByHighestPrice}>Highest price</Button>
&#13;
&#13;
&#13;

操作有效,他们按自己的意愿排序,在记录器中查看结果。

&#13;
&#13;
import React, {Component} from 'react';
import { PropTypes } from 'prop-types';

import { connect } from 'react-redux';
import { getProducts, sortProductsByLowestPrice, sortProductsByHighestPrice } from '../actions/productsActions';

import { Grid, Row, Button, Group, Loader } from 'semantic-ui-react';

import ProductItem from './ProductItem';

class ProductList extends React.Component {
  constructor(props) {
    super(props);
    
  }

  componentDidMount() {
    this.props.getProducts();
  }




  render() {
    
    return <div>
            <Button.Group className="buttonGroup">
                  <Button onClick={this.props.sortProductsByLowestPrice}>Lowest price</Button>
                  <Button onClick={this.props.sortProductsByHighestPrice}>Highest price</Button>
            </Button.Group>
            <div className="horizontal">
              {this.props.products ?
                 this.props.products.slice(0,5).map((product) => {
                return <ProductItem key={product._id} product={...product} />
                 }) : <Loader active inline />}
            </div>
           </div>
  }
}

ProductList.propTypes = {
  getProducts: PropTypes.func,
  products: PropTypes.array,
  sortProductsByLowestPrice: PropTypes.func,
  sortProductsByHighestPrice: PropTypes.func
}

const mapStateToProps = state => {
  return {
    products: state.products.products.products
 }
};

const mapDispatchToProps = (dispatch) => ({
    getProducts: () => dispatch(getProducts()),
    sortProductsByLowestPrice: () => dispatch(sortProductsByLowestPrice()),
    sortProductsByHighestPrice: () => dispatch(sortProductsByHighestPrice())
});



export default connect(mapStateToProps, mapDispatchToProps)(ProductList);
&#13;
&#13;
&#13;

0 个答案:

没有答案