Skip to content

Commit

Permalink
fix(useTransitionHook): update hook when options or criteria changes.…
Browse files Browse the repository at this point in the history
… Use a stable callback reference.
  • Loading branch information
christopherthielen authored and mergify[bot] committed Jan 6, 2020
1 parent be8e07f commit 57e593e
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/components/hooks/useTransitionHook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { curry, HookFn, StateParams, TransitionService, TransitionStateHookFn } from '@uirouter/core';
import { HookMatchCriteria, HookRegOptions, TransitionHookFn } from '@uirouter/core/lib/transition/interface';
import { useEffect, useState } from 'react';
import { useDeepObjectDiff } from './useDeepObjectDiff';
import { useRouter } from './useRouter';
import { useStableCallback } from './useStableCallback';

type HookName = 'onBefore' | 'onStart' | 'onSuccess' | 'onError' | 'onSuccess' | 'onFinish';
type StateHookName = 'onEnter' | 'onRetain' | 'onExit';
Expand All @@ -25,8 +27,9 @@ export function useTransitionHook(
options?: HookRegOptions
) {
const { transitionService } = useRouter();
const stableCallback = useStableCallback(callback);
useEffect(() => {
const deregister = transitionService[hookRegistrationFn](criteria, callback as any, options);
const deregister = transitionService[hookRegistrationFn](criteria, stableCallback as any, options);
return () => deregister();
}, [transitionService, hookRegistrationFn]);
}, [transitionService, hookRegistrationFn, useDeepObjectDiff(criteria), useDeepObjectDiff(options)]);
}

0 comments on commit 57e593e

Please sign in to comment.