EntityStore: fetch with useEntity automatically, accept refetch opt

environments/review-group-enti-e2lbwq/deployments/2781
Alex Gleason 2023-03-09 15:05:27 -06:00
rodzic ad583c89f8
commit fa2884c11b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
1 zmienionych plików z 9 dodań i 1 usunięć

Wyświetl plik

@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { useApi, useAppDispatch, useAppSelector } from 'soapbox/hooks';
@ -12,6 +12,8 @@ type EntityPath = [entityType: string, entityId: string]
interface UseEntityOpts<TEntity> {
/** A parser function that returns the desired type, or undefined if validation fails. */
parser?: (entity: unknown) => TEntity | undefined
/** Whether to refetch this entity every time the hook mounts, even if it's already in the store. */
refetch?: boolean
}
function useEntity<TEntity extends Entity>(
@ -42,6 +44,12 @@ function useEntity<TEntity extends Entity>(
});
};
useEffect(() => {
if (!entity || opts.refetch) {
fetchEntity();
}
}, []);
return {
entity,
fetchEntity,