Forest.js Core Documentation - v1.2.3
    Preparing search index...

    Function createRouter

    • createRouter

      Parameters

      • options: { isMultiInstance?: boolean } = {}

        Router options

        • OptionalisMultiInstance?: boolean

          When true, allows creating multiple router instances. Defaults to false.

          false
          
          const options: CreateRouterOptions = { isMultiInstance: true };
          

      Returns {
          router: {
              push: (path: string, state?: any) => void;
              back: () => void;
              replace: (path: string, state: any) => void;
              subscribe: <T = any>(
                  callback: (props: RouterProps<T>) => void,
              ) => () => void;
              unsubscribe: (unsubscribeFn: () => void) => void;
              get: () => null | RouterProps<any>;
          };
          routerStore: Store<null | RouterProps<any>>;
          destroyRouter: () => void;
      }

      Router instance

      • router: {
            push: (path: string, state?: any) => void;
            back: () => void;
            replace: (path: string, state: any) => void;
            subscribe: <T = any>(
                callback: (props: RouterProps<T>) => void,
            ) => () => void;
            unsubscribe: (unsubscribeFn: () => void) => void;
            get: () => null | RouterProps<any>;
        }

        Router instance

        Router instance

        const { router } = createRouter();
        
        • push: (path: string, state?: any) => void

          Push a new route

          const { router } = createRouter();
          router.push("/", { name: "John" });
        • back: () => void

          Go back to the previous route

          const { router } = createRouter();
          router.back();
        • replace: (path: string, state: any) => void

          Replace the current route

          const { router } = createRouter();
          router.replace("/", { name: "John" });
        • subscribe: <T = any>(callback: (props: RouterProps<T>) => void) => () => void

          Subscribe to router changes

          const { router } = createRouter();
          router.subscribe((props) => {
          console.log(props);
          });
        • unsubscribe: (unsubscribeFn: () => void) => void

          Unsubscribe from router changes

          const { router } = createRouter();
          const unsubscribe = router.subscribe((props) => {
          console.log(props);
          });
          unsubscribe();
        • get: () => null | RouterProps<any>

          Get the current router state

          const { router } = createRouter();
          const state = router.get()
      • routerStore: Store<null | RouterProps<any>>

        Router store

        Router store

        const { routerStore } = createRouter();
        
      • destroyRouter: () => void

        Destroy the router instance

        const { router } = createRouter();
        router.destroy();

      Create a router instance

      const { router, routerStore } = createRouter({ isMultiInstance: true });