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

272
Views
how to concat react-query previous data with new data

I have a hook, if I scroll down it will call for new data as like infinite scroll, but when I scroll down it calls the API for new data but it is removing the previous data. I want to concat with previous data. I think it is possible by the select method of react query but how can I get the previous data

const useActivityFeedData = (
  activityFeedPageIndex: number,
  pageSize = Config.defaultPageSize,
) =>
  useQuery(
    [ACTIVITY_FEED, activityFeedPageIndex],
    () => Api.user.getActivityFeedData(activityFeedPageIndex, pageSize),
    {
      initialData: {
        activityList: [],
      },
      keepPreviousData: true,
      select: (res: any) => ({
        activityList: res.activityList,
      }),
    },
  );
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Just create a array on mounting of component like this,

 let [data, setData] = useState([])
useEffect(()=>{
       
    },[])

Now get axios in same useEffect

useEffect(()=>{
       let data = []
axios.get(`....`).then(res=>{
setData(res.data)
})
    },[])

Now after scrolling when u get new Data, just concat it to previous array.

eg: axios.get(`...`).then(res=>{
let var = data.concat(res.data)
setData(var)
})

Then when u map Data, as it updates the page keeps scrolling.

Hope this helps : )

about 4 years ago · Juan Pablo Isaza Report

0

Why not something like:

const useActivityFeedData = (
  activityFeedPageIndex: number,
  pageSize = Config.defaultPageSize,
) =>
  const prevData = queryClient.getQueryData([ACTIVITY_FEED,activityFeedPageIndex]).
  useQuery(
    [ACTIVITY_FEED, activityFeedPageIndex],
    () => Api.user.getActivityFeedData(activityFeedPageIndex, pageSize),
    {
      initialData: {
        activityList: [],
      },
      keepPreviousData: true,
      select: (res: any) => ({
        activityList: res.activityList,
      }),
    },
  );

and then just return the prevData with your resposnse or assign the response to a variable from useQuery, and return it.

about 4 years ago · Juan Pablo Isaza Report

0

You are really looking for an infinite query - useInfiniteQuery is made for this exact use-case.

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!