Skip to content

Commit

Permalink
fix(core/presentation): Fix mount check -- useRef instead of useState
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Jul 25, 2019
1 parent 9c7885f commit f8c8f94
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ export function useLatestPromise<T>(
callback: () => IPromise<T>,
deps: DependencyList,
): [T, IRequestStatus, any, number] {
const mounted = useRef(false);
const requestInFlight = useRef<IPromise<T>>();
const [status, setStatus] = useState<IRequestStatus>('NONE');
const [result, setResult] = useState<T>();
const [error, setError] = useState<any>();
const [requestId, setRequestId] = useState(0);
const [mounted, setMounted] = useState(false);

useEffect(() => {
setMounted(true);
return () => setMounted(false);
mounted.current = true;
return () => (mounted.current = false);
}, []);

useEffect(() => {
const promise = callback();
const isCurrent = () => mounted.current === true && promise === requestInFlight.current;

// If no promise is returned from the callback, noop this effect.
if (!promise) {
return;
Expand All @@ -41,14 +43,14 @@ export function useLatestPromise<T>(
requestInFlight.current = promise;

const resolve = (newResult: T) => {
if (mounted && promise === requestInFlight.current) {
if (isCurrent()) {
setResult(newResult);
setStatus('RESOLVED');
}
};

const reject = (rejection: any) => {
if (mounted && promise === requestInFlight.current) {
if (isCurrent()) {
setError(rejection);
setStatus('REJECTED');
}
Expand Down

0 comments on commit f8c8f94

Please sign in to comment.