EntityStore: fetch with useEntity automatically, accept refetch opt
This commit is contained in:
parent
ad583c89f8
commit
fa2884c11b
1 changed files with 9 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { useApi, useAppDispatch, useAppSelector } from 'soapbox/hooks';
|
import { useApi, useAppDispatch, useAppSelector } from 'soapbox/hooks';
|
||||||
|
|
||||||
|
@ -12,6 +12,8 @@ type EntityPath = [entityType: string, entityId: string]
|
||||||
interface UseEntityOpts<TEntity> {
|
interface UseEntityOpts<TEntity> {
|
||||||
/** A parser function that returns the desired type, or undefined if validation fails. */
|
/** A parser function that returns the desired type, or undefined if validation fails. */
|
||||||
parser?: (entity: unknown) => TEntity | undefined
|
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>(
|
function useEntity<TEntity extends Entity>(
|
||||||
|
@ -42,6 +44,12 @@ function useEntity<TEntity extends Entity>(
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!entity || opts.refetch) {
|
||||||
|
fetchEntity();
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
entity,
|
entity,
|
||||||
fetchEntity,
|
fetchEntity,
|
||||||
|
|
Loading…
Reference in a new issue