react_hooks | @uirouter/react
Options
Menu

Module react_hooks

This is a collection of react hooks that can be used in functional components.

Index

Type aliases

HookName: "onBefore" | "onStart" | "onSuccess" | "onError" | "onSuccess" | "onFinish"
StateHookName: "onEnter" | "onRetain" | "onExit"

Functions

  • useCurrentStateAndParams(): object
  • A hook that returns the current state and parameter values.

  • A hook that returns the current state and parameter values.

    Each time the current state or parameter values change, the component will re-render with the new values.

    Example:

    function CurrentState() {
      const { state, params } = useCurrentStateAndParams();
      return <span>{state.name} ({JSON.stringify(params)})</span>;
    }

    Returns object

    • params: RawParams
    • state: StateDeclaration

  • useDeepObjectDiff(obj: object): number
  • Internal hook to support deep diffing in hook dependencies

  • Internal hook to support deep diffing in hook dependencies

    Parameters

    • obj object

    Returns number


  • useIsActive(stateName: string, params?: any, exact?: boolean): boolean
  • A hook that returns true if a given state is active.

  • A hook that returns true if a given state is active.

    Example:

    function ContactsLabel() {
     const isActive = useIsActive('contacts');
     return <span className={isActive ? 'active' : 'inactive'}>Contacts></span>
    }

    Example:

    function JoeLabel() {
     const isActive = useIsActive('contacts.contact', { contactId: 'joe' });
     return <span className={isActive ? 'active' : 'inactive'}>Joe></span>
    }

    Parameters

    • stateName string
      :

      the name of the state to check. Relative state names such as '.child' are supported. Relative states are resolved relative to the state that rendered the hook.

    • params: Default value  any = null
      :

      if present, the hook will only return true if all the provided parameter values match.

    • exact: Default value  boolean = false
      :

      when true, the hook returns true only when the state matches exactly. when false, returns true if the state matches, or any child state matches.

    Returns boolean


  • useOnStateChanged(onStateChangedCallback: function): void
  • A hook that invokes the provided callback whenever the current state changes.

  • A hook that invokes the provided callback whenever the current state changes.

    The callback receives the StateDeclaration and parameter values of the new current state.

    Example:

    function ShowCurrentState() {
      const [routerState, setRouterState] = useState('');
      useOnStateChanged((state) => setState(state.name);
      return <span>{routerState ? `state changed to ${routerState}` : null}</span>
    }

    Parameters

    • onStateChangedCallback function
      :

      a callback that receives the new current state and parameter values

        • (state: StateDeclaration, params: RawParams): void
        • Parameters

          • state StateDeclaration
          • params RawParams

          Returns void

    Returns void


  • Returns UIViewAddress


  • useRouter(): UIRouter
  • A hook that returns the UIRouter instance

  • A hook that returns the UIRouter instance

    Example:

    const FormSubmit() {
      const router = useRouter();
      const form = useContext(FormFromContext);
      function submit() {
        validateForm(form)
          .then(submitForm)
          .then(() => router.stateService.go('home'));
      }
    
      return <button onClick={submit}>Submit form</button>;
    }

    Returns UIRouter


  • useSref(stateName: string, params?: object, options?: TransitionOptions): LinkProps
  • A hook to create a link to a state.

  • A hook to create a link to a state.

    This hook returns link (anchor tag) props for a given state reference. The resulting props can be spread onto an anchor tag.

    The props returned from this hook are:

    • href: the browser URL of the referenced state
    • onClick: a mouse event handler that will active the referenced state

    Example:

    function HomeLink() {
      const sref = useSref('home');
      return <a {...sref}>Home</a>
    }

    Example:

    function UserLink({ userId, username }) {
      const sref = useSref('users.user', { userId: userId });
      return <a {...sref}>{username}</a>
    }

    Parameters

    • stateName string
      :

      The name of the state to link to

    • params: Default value  object = {}
      :

      Any parameter values

    • options: Default value  TransitionOptions = {}
      :

      Transition options used when the onClick handler fires.

    Returns LinkProps


  • useSrefActive(stateName: string, params?: object, activeClass: string, options?: TransitionOptions): ActiveLinkProps
  • A hook to create a link to a state and track its active status.

  • A hook to create a link to a state and track its active status.

    This hook returns link (anchor tag) props for a given state reference. The resulting props can be spread onto an anchor tag. If the referenced state (and params) is active, then the activeClass is returned as the className prop.

    The props returned from this hook are:

    • href: the browser URL of the referenced state
    • onClick: a mouse event handler that will active the referenced state
    • className: the activeClass parameter when the state (or any child state) is active, otherwise an empty string

    Example:

    function HomeLink() {
      const sref = useSref('home', null, 'active');
      return <a {...sref}>Home</a>
    }

    Example:

    function UserLink({ userId, username }) {
      const sref = useSref('users.user', { userId: userId }, 'active');
      return <a {...sref}>{username}</a>
    }

    This hook is a variation of the useSref hook.

    Parameters

    • stateName string
      :

      The name of the state to link to

    • params: Default value  object = {}
      :

      Any parameter values

    • activeClass string
      :

      A css class string to use when the state is active

    • options: Default value  TransitionOptions = {}
      :

      Transition options used when the onClick handler fires.

    Returns ActiveLinkProps


  • useSrefActiveExact(stateName: string, params?: object, activeClass: string, options?: TransitionOptions): ActiveLinkProps
  • A hook to create a link to a state and track its active status.

  • A hook to create a link to a state and track its active status.

    This hook returns link (anchor tag) props for a given state reference. The resulting props can be spread onto an anchor tag. If the referenced state (and params) is active, then the activeClass is returned as the className prop.

    The props returned from this hook are:

    • href: the browser URL of the referenced state
    • onClick: a mouse event handler that will active the referenced state
    • className: the activeClass parameter when the state is active, otherwise an empty string

    Example:

    function HomeLink() {
      const sref = useSref('home', null, 'active');
      return <a {...sref}>Home</a>
    }

    Example:

    function UserLink({ userId, username }) {
      const sref = useSref('users.user', { userId: userId }, 'active');
      return <a {...sref}>{username}</a>
    }

    This hook is a variation of the useSrefActive hook. This variation does not render the activeClass if a child state is active.

    Parameters

    • stateName string
      :

      The name of the state to link to

    • params: Default value  object = {}
      :

      Any parameter values

    • activeClass string
      :

      A css class string to use when the state is active

    • options: Default value  TransitionOptions = {}
      :

      Transition options used when the onClick handler fires.

    Returns ActiveLinkProps


  • useStableCallback<T>(unstableCallback: T): T
  • Returns a stabilized callback reference which delegates to the most recent unstable callback

  • Returns a stabilized callback reference which delegates to the most recent unstable callback

    This is similar to useCallback, but allows unstableCallback to access the most recent values from the closure. This can be useful if the callback is being stored long term, such as in the Transition Hook registry.

    Example:

    const latestValueFromProps = props.value
    const transitionHook = useStableCallback(() => console.log(latestValueFromProps));
    useEffect(() => {
      const deregister = transitionService.onBefore({ exiting: 'someState' }, transitionHook);
      return () => deregister();
    }, []);

    Type parameters

    • T: Function

    Parameters

    • unstableCallback T

    Returns T


  • useTransitionHook(hookName: HookName, criteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): any
  • A React hook that registers a UI-Router Transition Hook and manages its lifecycle.

  • useTransitionHook(hookName: StateHookName, criteria: HookMatchCriteria, callback: TransitionStateHookFn, options?: HookRegOptions): any
  • A React hook that registers a UI-Router Transition Hook and manages its lifecycle.

    This hook can be used to register a Transition Hook with UI-Router from a component. The Transition Hook will be automatically de-registered when the component unmounts. The Transition Hook will receive the current Transition object (like all Transition Hooks).

    Example:

    function DisallowExitUntilVerified() {
      const [allowExit, setAllowExit] = useState(false);
      useTransitionHook('onBefore', {}, transition => {
        return allowExit;
      });
    
      if (canExit) {
        return <span>OK, the current state can be exited!</span>
      }
      return (
        <div>
          The current state can't be exited until you click this button:
          <button onClick={() => setAllowExit(true)}>Allow Exit</button>
         </div>
      )
    }

    Parameters

    • hookName HookName
      :

      the name of the lifecycle event

    • criteria HookMatchCriteria
      :

      the transition criteria object

    • callback TransitionHookFn
      :

      the callback to invoke

    • options: Optional  HookRegOptions
      :

      transition hook options

    Returns any


  • Parameters

    • hookName StateHookName
    • criteria HookMatchCriteria
    • callback TransitionStateHookFn
    • options: Optional  HookRegOptions

    Returns any


Generated using TypeDoc