Home > Back-end >  Filter in react.js
Filter in react.js

Time:11-12

I'm trying to make a filter of all my "localtypes" but when I check the console on my browser it shows me an empty array. I want to get to the localtypes propertys of the API I'm using

I tried to use this filter

 const type = (category) => {
   const clonArray = [...restaurants]
   const filter = clonArray.filter((restaurant) => {
     return restaurant.localtype == category;
   })
   setRestaurants(filter);
  }

sending the props to another component "Filter" as

categories={() => type()}

but when i get to these props in the Filter component i get an empty array

onClick={() => {categories("farmacia")}}>

I want to know how to access to the props of "categories"

CodePudding user response:

You can do it in below order. First thing to note is that .filter does not mutate the original array, so you can use it directly.

You need to pass the value with function to the Filter component.

onClick={() => categories('farmacia'}

categories={(cat) => type(cat)}

const type = (category) => {
   const filter = clonArray.filter((restaurant) => {
     return restaurant.localtype === category;
   })
   setRestaurants(filter);
}

  • Related