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

110
Views
React useState hook - set value

I'm new to the whole React and React hooks staff, I'm trying to reset the value in useState to default if new filters are selected.

const [apartments, setApartments] = React.useState([])
const [page, setPage] = React.useState(1)
const fetchData = (onInit = true, loadMore = true) => {
        let setQuery = ''
        if (!loadMore) {
            setApartments([]) // <--- how to reset to empty?
            setPage(1) // <--- Not setting value to 1
        }
        if (!onInit || query()) {
            filtersWrapper.current.querySelectorAll('.select-wrapper select').forEach(item => {
                if (item.value) {
                    setQuery += `&${ item.name }=${ item.value }`
                }
            })
        }
        fetch(apiUrl + `?page=${ page }&pageSize=12${ setQuery }`)
            .then(res => res.json())
            .then(data => {
                setApartments([...apartments, ...data.apartments] || [])
                setHasNextPage(data.hasNextPage)
                setPage(prev => prev + 1)
            })
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Identify the page to pass to fetch from the loadMore argument, not from the state value. Similarly, identify from the argument what to pass to setApartments. This way, all the state gets updated at once, inside the fetch.

const fetchData = (onInit = true, loadMore = true) => {
    let setQuery = ''
    if (!onInit || query()) {
        filtersWrapper.current.querySelectorAll('.select-wrapper select').forEach(item => {
            if (item.value) {
                setQuery += `&${item.name}=${item.value}`
            }
        })
    }
    const pageToNavigateTo = loadMore ? page : 1;
    fetch(apiUrl + `?page=${pageToNavigateTo}&pageSize=12${setQuery}`)
        .then(res => res.json())
        .then(data => {
            const initialApartments = loadMore ? apartments : [];
            setApartments([...initialApartments, ...data.apartments]);
            setHasNextPage(data.hasNextPage);
            setPage(pageToNavigateTo + 1);
        })
        // .catch(handleErrors); // don't forget this
}

I'd also recommend changing the

filtersWrapper.current.querySelectorAll('.select-wrapper select').forEach

from DOM methods to setting React state instead.

about 4 years ago · Juan Pablo Isaza Report
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!