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

169
Views
How to rerender useEffect when state change

Need to update the useEffect call when I update the state. can anyone correct the code? Currently is working like this screenshot the updated data is rendering after pre-render data. what is needed is when I click on any filter every it renders new data. Check screenshot

Here is my code :

const PAGE_NUMBER =1;

export const SimpleInfiniteList = () => {

  const [img, setImg] = useState('')
  const [page, setPage] = useState(PAGE_NUMBER)
  const iddata = img.slice(-1)
  const [filter, setFilter] = useState('top')
  const [, forceUpdate] = useReducer(x => x + 1, 0);


useEffect(() => {
  const filt = () => {

    if(filter > 0){
      filt()
      
    }

  }

}, [])
  

 

  useEffect( () => {
    forceUpdate()
   
  fetch(`https://www.reddit.com/r/ProgrammerHumor/${filter}.json?after=${ iddata.length > 0 ? iddata.map(id => `t3_${id.data.id}`): ''}&page=${page}&limit=2`)
  .then(response => response.json())
  .then(data => setImg([...img, ...data.data.children], 
    )
  )
}, [filter,page])



console.log(img)



const scrollToEnd = () => {
  setPage(page + 1) 
}
window.onscroll = function () {
  const bottom =  Math.ceil(window.innerHeight + window.scrollY) >= document.documentElement.scrollHeight
  
  if (bottom,
     Math.ceil(window.innerHeight + window.scrollY) >= document.documentElement.scrollHeight
    ) {
      scrollToEnd() 
      console.log('loading....')
  }
}
}
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!