首页 文章

使用筛选项更新过滤器

提问于
浏览
0

我正在尝试将 productList 上的过滤器中的数据添加到我的redux状态 filteredProducts

它几乎正常工作,它将添加过滤项的最后状态,但我无法使其与当前状态一起工作

如何更新它以便 productFilter 的当前状态将更新为 filteredProducts

redux state

产品:[],过滤产品:[]

productList.js

class ProductsList extends React.Component {

  render() {
    const { e, p, filteredColors, filteredSizes,  match, products } = this.props
    const productFilter = this.props.products.filter(products =>
      (
        (filteredColors.length >= 1 && filteredSizes.length < 1 && products.cat === match.params.subCatId) && filteredColors.includes(products.color) ||
        (filteredSizes.length >= 1 && filteredColors.length < 1 && products.cat === match.params.subCatId) && filteredSizes.includes(products.size) ||

        (filteredSizes.length >= 1 && filteredColors.length >= 1 && products.cat === match.params.subCatId) && filteredColors.includes(products.color) && filteredSizes.includes(products.size)) ||
        (filteredSizes.length < 1 && filteredColors.length < 1 && products.cat === match.params.subCatId)
      )


    return(
      <Section>
        <Container>
          <Grid>
              <button onClick={()=>this.props.setFilteredProducts('adjda')}>djndnd</button>
            {productFilter && productFilter.map(filteredProduct =>
              <Cell key={filteredProduct.id}>
                <ProductListCard e={e} p={p} match={match} {...filteredProduct} />
              </Cell>
            )}
          </Grid>
        </Container>
        <Filters>
          <Filter productFilter={productFilter} handleUpdateFilter={this.handleUpdateFilter} />
        </Filters>
      </Section>
    )
  }
}

const mapStateToProps = state => ({
  filteredProducts: state.filteredProducts
});

export default connect(mapStateToProps, actionCreators)(ProductsList);

filter.js

class Filter extends React.Component {
  state = {
    showTab: ''
  }
  handleFilterColors = colorOption => {
    this.props.colors.includes(colorOption) ? this.props.removeColor(colorOption) : this.props.addColor(colorOption)
  }

  handleFilterSizes = sizeOption => {
    this.props.sizes.includes(sizeOption) ? this.props.removeSize(sizeOption) : this.props.addSize(sizeOption)
    this.props.setFilteredProducts(this.props.productFilter)
    console.log(this.props.productFilter)
  }

  handleShowTab = id => {
    const stateId = this.state.showTab
    this.setState({
      showTab: id === stateId ? false : id
    })
  }

  render() {
    const { colors, sizes, e, p } = this.props
    const colorOptions = ['gold', 'silver', 'bronze', 'platinum']
    const sizeOptions = ['7', '8', '9', '10']

    const { showTab } = this.state
    console.log(showTab)
    return(
      <Wrap>
        <ShowMobile>
          <Cell onClick={() => this.handleShowTab(1)}><FilterButton active={showTab === 1}>Filters</FilterButton></Cell>
          <Cell onClick={() => this.handleShowTab(2)}><FilterButton active={showTab === 2}>Sort by</FilterButton></Cell>
          {showTab === 1 &&
            <Bg>
            <FilterWrap>
                <FilterOptions>
                  <Title>Filter by Colour</Title>
                  {colorOptions.map((colorOption,index) =>
                    <FilterCell key={index}>
                      <ColorOption active={colors.includes(colorOption)}  onClick={()=> this.handleFilterColors(colorOptions[index])}>
                        {colorOption}
                        <Color />
                      </ColorOption>
                    </FilterCell>
                  )}
                </FilterOptions>
              </FilterWrap>

              <FilterWrap>
                <FilterOptions>
                  <Title>Filter by Size</Title>
                  {sizeOptions.map((sizeOption,index) =>
                    <FilterCell key={index}>
                      <ColorOption active={sizes.includes(sizeOption)} onClick={()=> this.handleFilterSizes(sizeOptions[index])}>
                        {sizeOption}
                        <Color />
                      </ColorOption>
                    </FilterCell>
                  )}
                </FilterOptions>
              </FilterWrap>

            </Bg>
          }


          {showTab === 2 &&
            <Bg>

            </Bg>
          }
        </ShowMobile>
        <ShowDesktop>
          <Cell>
            <DesktopGrid>
              <DesktopCell>
                <Title>sss</Title>
                <FilterWrap>
                  {colorOptions.map((colorOption,index) =>
                    <FilterCell key={index}>{colorOption}</FilterCell>
                  )}
                </FilterWrap>
              </DesktopCell>
              <DesktopCell>
                <Title>sss</Title>
                <FilterWrap>
                  {sizeOptions.map((sizeOption,index) =>
                    <FilterCell key={index}>
                      <SizeOption>
                        {sizeOption}
                      </SizeOption>
                    </FilterCell>
                  )}
                </FilterWrap>
              </DesktopCell>
            </DesktopGrid>
          </Cell>
          <Cell>

          </Cell>
        </ShowDesktop>


      </Wrap>
    )
  }
}

const mapStateToProps = state => ({
  colors: state.filters.colors,
  sizes: state.filters.sizes,
  filteredProducts: state.filteredProducts
})

export default connect(mapStateToProps, actionCreators)(Filter);

filterActions.js

export function setFilteredProducts(filteredProducts) {
  return {
    type: FILTERED_PRODUCTS,
    filteredProducts
  }
}

1 回答

  • 0

    我已经通过将productFIlter移动到 filter.js 并检查过滤器是否在componentDidUpdate中更改来解决这个问题

    filter.js

    componentDidUpdate(prevProps) {
      const {filteredColors, filteredSizes,  match } = this.props
      const productFilter = this.props.products.filter(products =>
        (
          (filteredColors.length >= 1 && filteredSizes.length < 1 && products.cat === match.params.subCatId) && filteredColors.includes(products.color) ||
          (filteredSizes.length >= 1 && filteredColors.length < 1 && products.cat === match.params.subCatId) && filteredSizes.includes(products.size) ||
    
          (filteredSizes.length >= 1 && filteredColors.length >= 1 && products.cat === match.params.subCatId) && filteredColors.includes(products.color) && filteredSizes.includes(products.size)) ||
          (filteredSizes.length < 1 && filteredColors.length < 1 && products.cat === match.params.subCatId)
        )
        console.log('product filter',productFilter)
        if(prevProps.filteredColors !== this.props.filteredColors || prevProps.filteredSizes !== this.props.filteredSizes) {
          this.props.setFilteredProducts(productFilter)
        }
    }
    

相关问题