diff --git a/src/entity-store/hooks/useEntityLookup.ts b/src/entity-store/hooks/useEntityLookup.ts index a54882d6f..3ae97b704 100644 --- a/src/entity-store/hooks/useEntityLookup.ts +++ b/src/entity-store/hooks/useEntityLookup.ts @@ -25,10 +25,11 @@ function useEntityLookup( const { schema = z.custom() } = opts; const dispatch = useAppDispatch(); + const [fetchedEntity, setFetchedEntity] = useState(); const [isFetching, setPromise] = useLoading(true); const [error, setError] = useState(); - const entity = useAppSelector(state => findEntity(state, entityType, lookupFn)); + const entity = useAppSelector(state => findEntity(state, entityType, lookupFn) ?? fetchedEntity); const isEnabled = opts.enabled ?? true; const isLoading = isFetching && !entity; @@ -36,6 +37,7 @@ function useEntityLookup( try { const response = await setPromise(entityFn()); const entity = schema.parse(response.data); + setFetchedEntity(entity); dispatch(importEntities([entity], entityType)); } catch (e) { setError(e);