components | @uirouter/react
Options
Menu

Module components

UI-Router React Components and their APIs:

  • UIRouter: Main router component
  • UIView: A viewport for routed components
  • UISref: A state ref to a target state; navigates when clicked
  • UISrefActive: Adds a css class when a UISref's target state (or a child state) is active

Index

Type aliases

RenderPropCallback: function

Function type for UIViewProps.render

Function type for UIViewProps.render

If the render function prop is provided, the UIView will use it instead of rendering the component by itself.

Type declaration

    • (Component: ComponentType<any>, Props: any): Element | null
    • Parameters

      • Component ComponentType<any>
      • Props any

      Returns Element | null

Variables

TransitionPropCollisionError: string = '`transition` cannot be used as resolve token. ' +'Please rename your resolve to avoid conflicts with the router transition.'
UIRouterConsumer: ExoticComponent<ConsumerProps<UIRouter>> = UIRouterContext.Consumer
deprecated

use useRouter or React.useContext(UIRouterContext)

UIRouterContext: Context<UIRouter> = React.createContext<_UIRouter>(undefined)

This React Context component lets you access the UIRouter instance anywhere in the component tree

This React Context component lets you access the UIRouter instance anywhere in the component tree

When using hooks, use useRouter instead.

Example:

<UIRouterContext.Consumer>
 {router => <MyComponent router={router} />}
</UIRouterContext.Consumer>
UISrefActiveContext: Context<function> = React.createContext<AddStateInfoFn>(rootAddStateInfoFn)
UIViewConsumer: ExoticComponent<ConsumerProps<UIViewAddress>> = UIViewContext.Consumer
deprecated

use useParentView or React.useContext(UIViewContext)

UIViewContext: Context<UIViewAddress> = createContext<UIViewAddress>(undefined)
View: ForwardRefExoticComponent<UIViewProps | RefAttributes<unknown>> = forwardRef(function View(props: UIViewProps, forwardedRef) {const { children, render, className, style } = props;const router = useRouter();const parent = useParentView();const creationContext = parent.context;const { viewConfig, configUpdated } = useViewConfig();const component = useMemo(() => viewConfig?.viewDecl?.component, [viewConfig]);const name = props.name || '$default';const fqn = parent.fqn ? parent.fqn + '.' + name : name;const id = useMemo(() => ++viewIdCounter, []);// This object contains all the metadata for this UIViewconst uiViewData: ActiveUIView = useMemo(() => {return { $type: 'react', id, name, fqn, creationContext, configUpdated, config: viewConfig as ViewConfig };}, [id, name, fqn, parent, creationContext, viewConfig]);const viewContext: ViewContext = viewConfig?.viewDecl?.$context;const stateName: string = viewContext?.name;const uiViewAddress: UIViewAddress = { fqn, context: viewContext };const resolveContext = useMemo(() => (viewConfig ? new ResolveContext(viewConfig.path) : undefined), [viewConfig]);const injector = useMemo(() => resolveContext?.injector(), [resolveContext]);const transition = useMemo(() => injector?.get(Transition), [injector]);const resolves = useResolvesWithStringTokens(resolveContext, injector);const childProps = useRoutedComponentProps(router,stateName,viewConfig,component,resolves,className,style,transition);// temporarily expose a ref with an API on it for @uirouter/react-hybrid to useuseReactHybridApi(forwardedRef, uiViewData, uiViewAddress);// Register/deregister any time the uiViewData changesuseEffect(() => router.viewService.registerUIView(uiViewData), [uiViewData]);const childElement =!component && isValidElement(children)? cloneElement(children, childProps): createElement(component || 'div', childProps);// if a render function is passed, use that. otherwise render the component normallyconst ChildOrRenderFunction =typeof render !== 'undefined' && component ? render(component, childProps) : childElement;return <UIViewContext.Provider value={uiViewAddress}>{ChildOrRenderFunction}</UIViewContext.Provider>;})
viewIdCounter: number = 0

Functions

  • UIRouter(props: UIRouterProps): Element
  • This is the root UIRouter component, needed for initialising the router and setting up configuration properly. Every other component from this library needs to be a descendant of <UIRouter>, so ideally you want to use it as root of your app.

  • This is the root UIRouter component, needed for initialising the router and setting up configuration properly. Every other component from this library needs to be a descendant of <UIRouter>, so ideally you want to use it as root of your app.

    Config

    There are two ways to set up the router: you can either bootstrap it manually and pass the instance to the component, or pass the necessary information and let the component do it for you.

    Component Setup (suggested)

    Setting up the router via this component is pretty straightforward:

    const Home = () => <div>Home</div>;
    
    const states = [{
      name: 'home',
      component: Home,
      url: '/home'
    }];
    
    const plugins = [pushStateLocationPlugin];
    
    ReactDOM.render(
      <UIRouter plugins={plugins} states={states}>
        <UIView />
      </UIRouter>,
      document.getElementById('root'),
    );

    Optionally, you may want to access the router instance once to setup additional configuration, like registering [[TransitionHook]]s. To do so, you may pass a config function that will be called with the newly created router instance as argument:

    const config = router => {
      // register home state as the intial one
      router.urlService.rules.initial({ state: 'home' });
    }
    
    ReactDOM.render(
      <UIRouter plugins={plugins} states={states} config={config}>
        <UIView />
      </UIRouter>,
      document.getElementById('root'),
    );

    Manual Setup (advanced)

    Alternatevely you may setup the router manually (i.e. exctracting the router configuration to another file). You can do that by createing a new instance of the router and pass it to the component, this way the component will skip the previous props and just use the provided instance.

    NB: since you are manually bootstrapping the router, you must register the servicesPlugin as well as the location plugin of your choice (in this example the pushStateLocationPlugin).

    const router = new UIRouterReact();
    // activate plugins
    router.plugin(servicesPlugin);
    router.plugin(pushStateLocationPlugin);
    // register states
    router.stateRegistry.register(someState);
    // start the router
    router.start();
    
    ReactDOM.render(
      <UIRouter router={router}>
        <UIView />
      </UIRouter>,
      document.getElementById("root")
    );

    Parameters

    Returns Element


  • UISref(__namedParameters: object): DetailedReactHTMLElement<any, HTMLElement>
  • This component lets create links to router states, allowing the user to navigate through the application. It works well together with <a> and <button> nodes.

  • This component lets create links to router states, allowing the user to navigate through the application. It works well together with <a> and <button> nodes.

    You can wrap your anchor/button and define the router state you want it to link to via props. If the state has an associated URL, it will automatically generate and update the href attribute. Cliking its children will trigger a state transition with the optional parameters.

    Example:

    // state definition
    const state = {
      name: 'catalog',
      url: '/shop/catalog?productId',
      component: Catalog
    }
    
    // UISref component
    <UISref to="catalog" params=>
      <a>Product 103</a>
    </UISref>
    
    // rendered dom
    <a href="#/shop/catalog?productId=103">Product 103</a>

    It will also respect the default behavior when the user Cmd+Click / Ctrl+Click on the link by canceling the transition event and opening a new tab instead.

    Parameters

    • __namedParameters object
      • children: any
      • className: string
      • options: TransitionOptions
      • params: object
      • to: string

    Returns DetailedReactHTMLElement<any, HTMLElement>


  • UISrefActive(__namedParameters: object): Element
  • A component working alongside <a href="components.html#uisref">UISref</a> to add classes to its child element when one of the included <a href="components.html#uisref">UISref</a>'s state is active, and removing them when it is inactive.

  • A component working alongside <a href="components.html#uisref">UISref</a> to add classes to its child element when one of the included <a href="components.html#uisref">UISref</a>'s state is active, and removing them when it is inactive.

    The primary use-case is to simplify the special appearance of navigation menus relying on [[<UISref>]], by having the "active" state's menu button appear different, distinguishing it from the inactive menu items.

    It will register every nested [[<UISref>]] and add the class to its child every time one of the states is active.

    <UISrefActive class="active-item">
      <UISref to="homestate"><a class="menu-item">Home</a></UISref>
    </UISrefActive>
    
    // rendered when state is inactive
    <a href="/path/to/homestate" class="menu-item">Home</a>
    
    // rendered when state is active
    <a href="/path/to/homestate" class="menu-item active-item">Home</a>

    Parameters

    • __namedParameters object
      • children: any
      • className: string
      • classToApply: string
      • exact: Boolean

    Returns Element


  • rootAddStateInfoFn(): (Anonymous function)
  • Returns (Anonymous function)


  • useRoutedComponentProps(router: UIRouter, stateName: string, viewConfig: ViewConfig, component: FunctionComponent<any> | ComponentClass<any> | ClassicComponentClass<any>, resolves: TypedMap<any> | __type, className: string, style: Object, transition: any): UIViewInjectedProps | object
  • Parameters

    • router UIRouter
    • stateName string
    • viewConfig ViewConfig
    • component FunctionComponent<any> | ComponentClass<any> | ClassicComponentClass<any>
    • resolves TypedMap<any> | __type
    • className string
    • style Object
    • transition any

    Returns UIViewInjectedProps | object


Generated using TypeDoc