EntityStore: fetch with useEntity automatically, accept refetch opt

environments/review-group-enti-e2lbwq/deployments/2781
Alex Gleason 2 years ago
parent ad583c89f8
commit fa2884c11b
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

@ -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…
Cancel
Save