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

232
Views
How can I synchronize data from two different queries, where one is being transformed in useEffect?

I have an array and map being fetched from two different RTK Queries. Array A contains metaData (Id's) used to access the values in map B. Hence, their recency needs to be synchronized so that every value in B has some ID in array A.

I fetch both data from the server, but then apply some filters to map B such that the number of values is potentially reduced. This is done like so:

const queryAResult = useRTKQueryA(); // arrayA = queryAResult.data
const queryBResult = userRTKQueryB(); // mapB = queryBResult.data

const [filteredMapB, setFilteredMapB] = useState({});

// UseEffect is used here because queryBResult changes as the query is refetched under the appropriate conditions.
useEffect(() => {
  if (queryBResult.data) {
    const mapB = queryBResult.data;
    const filtered = filter(mapB); // Generic filter
    setFilteredMapB[filtered];
  }

}, [queryBResult.data]); // mapB = queryBResult.data


// Here I render a component which is dependent on using arrayA to make accesses to filteredMapB. 

The issue im facing is that, while I can listen to the query results for both arrayA and mapB to make sure they're both available (through data, isLoading, isFetching, etc), im unsure of how to make sure that the filtering process in useEffect has occurred before using arrayA to access filteredMapB. Currently the problem occurs because I use the most recently available version of arrayA to access filteredMapB (which is not alway the most recent, since the filtering takes time), so arrayA ends up containing elements not in filteredMapB.

How can I synchronize these? Ideally the solution would lend itself well to a boolean expression I could use to conditionally render the list or a loading spinner.

I have thought about putting both arrays in local state variables and using useCallback to create getters for the two arrays which would return synchronized values by using the dependency array, but would prefer a different solution.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I would not use useEffect at all here. This is something where useMemo comes in a lot more handy, as it happens synchronously and you will never have a render in-between.

const queryAResult = useRTKQueryA(); // arrayA = queryAResult.data
const queryBResult = userRTKQueryB(); // mapB = queryBResult.data

const filteredMapB = useMemo(() => 
  if (queryBResult.data) {
    const mapB = queryBResult.data;
    const filtered = filter(mapB); // Generic filter
    return filtered
  }
}, [queryBResult.data]); // mapB = queryBResult.data


if (!queryAResult || !filteredMapB) {
  return <Spinner />
}

// normal rendering
about 4 years ago · Juan Pablo Isaza Report

0

it looks like your ArrayA is also changing, so maybe include ArrayA in useEffect as well in your 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!