Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda