首页 > 解决方案 > react js中的过滤器错误无法读取reactjs中未定义的属性'indexOf'

问题描述

我正在使用过滤器过滤一个对象数组,我通过 axios 从 api 获取它并将其存储在 redux 状态它适用于映射但使用过滤器它会给出此错误 Cannot read property 'indexOf' of undefined in reactjs

    class ProductsList extends Component {

    constructor(props) {
        super(props)
      
       this.state = {
          search : '' 

        } 
    }

   searchUpdate(event) {
   this.setState({search: event.target.value.substr(0,20)})
} 

async componentDidMount() {

  
  const { fetchProducts } = this.props;
  
  await fetchProducts();
  

  }
  async componentDidUpdate() {
    
  const { fetchProducts } = this.props;
  
  await fetchProducts();
    
   }





    render() {
    //The problem with this filter method however if I don't use it it works will
let productsFiltered = this.props.products.filter((itemProduct)=>{
  return  itemProduct.nameEn.indexOf(this.state.search) !== -1
  } )

        

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

}

…………

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

标签: javascriptreactjsreduxfilter

解决方案


推荐阅读