Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Add and remove functions in javascript array (React-Hooks)

I have a filtering system where I use an array to add or remove the filtering methods. But my state is not working properly or i've missed something.

const [filters, setFilters] = useState([]);
const [creatorFilter, setCreatorFilter] = useState(null);

const handleCreatorFilter =(filter) => {
        setCreatorFilter(filter);

        if (filter === 'structures' && filters.indexOf(byStructureFilter) === -1) {
            setFilters([...filters, byStructureFilter]);
        }

        if (filter === 'members' && filters.indexOf(byMemberFilter) === -1) {
            setFilters([...filters, byMemberFilter]);
        }

        if (filter === 'all') {
            setFilters(filters.filter(el => el !== byStructureFilter || el !== byMemberFilter));
        }
    };

    const byStructureFilter = (item) => {
        return item.relationships.structure.data
    };

    const byMemberFilter = (item) => {
        return item.relationships.user.data && !item.relationships.structure.data
    }; 

Here the two buttons calling that handleCreatorFilter for filtering

<button onClick={() => creatorFilter === 'structures' ? handleCreatorFilter('all') : handleCreatorFilter('structures')}>Structures officielles</button>
<button onClick={() => creatorFilter === 'members' ? handleCreatorFilter('all') : handleCreatorFilter('members')} >Membres Wekomkom</button>

The things is for adding filtering methods to the array it works fine but not when the filter is set to all (removing the filtering methods). Do you see something wrong in my logic ?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!