⛄ Snowberb ⛄ – 12-56 May 26
Im trying to do a multi-filter function on React, and I'm really stuck. I have diverse filters: and I also have a lot of objects with those properties and more, called
bonuses: IBonusMy objective is that I have to apply all these filters to the
bonusesobject at once, depending on what the user selects, for example,
gananciacould have a
minof 20€ and a
maxof 100€ etc. How can I do this? I could only achieve 1 filter at a time, I have no idea how I can chain all of them
You don't chain the filters You could, but it's not necessary Rather, compose the predicates That is to say, you have a function which will take one of the filters and the object and returns true or false (this is the predicate) Create one such a predicate for every filter And then you can use all of these predicates in the
.filterfunction, so only the objects for which
truewill be returned for all of the predicates will be kept
could you explain this one more in depth please?
Can you send the interface
This is the type of filter im talking about btw (styling not finished)
Imagine the state is then you can create a predicate for each then use these predicates as such And you can AND (&&) all of these predicates for every filter you have in there Be aware that if you have a filter that can be unselected / hold no value that the predicate should return true for those cases as well
This way It would work only after hitting a button like "Apply filters"? Would it be a lot more complex to filter
bonusesevery time a user selects a filter?
That depends on when you choose to update the state, this part is filtering the state once the filters have been selected
Complex in terms of what?
No it is not, it does not change the piece of code Im suggesting That's about when you update the state You can choose to update the state as the user clicks and updates each individual filter (controlled components) or you choose to only sync the state once the user clicks the button "apply filter"
waht im trying to say is that if you select for example
bonuseswould only remain that house and if I deselect that filter i'd have no way of retrieving the other objects
Don't update the state of
I make a copy of it?
You don't even have to Just filter the
bonuseswill always have all the bonuses
oh I get it
There is no need to overwrite that state you just lose information that way
true true okay let me try this thank you so much sebas
And what you're supposed to do is deriving the filtered view for your state npnp
you have no idea how many times you helped me in this server 🤣 how do I print the array? If in my component I'd have to make a copy of
bonusesdont I? :/
.filterwill create a new array Use that array
and last thing, what if I have the list paginated like this? I should have said it before sorry, that's why I am asking if I should make a copy and how I must manage that
filteredItemsis a copy of
I have to go, hope someone else can help you from here
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?Recommended Posts
Tailwind suckswhat is the difference between tailwind and pure css ? nothing just another way of writing css withVuNguyen – 20-31 May 24Hi, I'm building a video player and I'm curious how `seeking` works. Because I'm using my own made s✅ – venus – 08-46 May 24Hey, I am trying to catch all mdx headings (`## First Heading`, `### Third Heading`, etc.) but only VuNguyen – 03-08 May 24Hi, I'm trying to publish npm package, but the bundle size seem to big. So I wanna blacklist files h