# Navigation

> Go to any screen, go back or reset your app's navigation from the desktop app, with React Navigation or Expo Router. It is opt-in.

Navigation adds commands to the [Commands](/docs/desktop/commands/) panel that move your app around: open a screen that has no button leading to it yet, with the params you choose, go back, or reset the stack. It can also log every screen change in the Console.

[Using Navigation in the desktop app](/docs/desktop/navigation/)

## Setup

**Accepts** `false | NavigationPluginConfig` Opt-in

| Option         | Type                         | Default | Description                                                                                                                                |
| -------------- | ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `ref` required | `NavigationContainerRefLike` | —       | The container ref from `useNavigationContainerRef()`, from React Navigation or Expo Router. Call `init` in a `useEffect` once you have it. |

Caution

The `navigation` plugin needs the ref from `useNavigationContainerRef()`, so call `init` from a `useEffect` in the component that renders your navigation container, and pass the ref to that container too.

* React Navigation

  *use-xerlock-navigation.ts*

  ```ts
  import { useNavigationContainerRef } from '@react-navigation/native';
  import { init } from '@xerlock/client';
  import { useEffect } from 'react';

  export const useXerlockNavigation = () => {
    const navigationRef = useNavigationContainerRef();

    useEffect(() => {
      init({
        navigation: { ref: navigationRef },
      });
    }, [navigationRef]);

    return navigationRef;
  };
  ```

  Show the full example

  The hook returns the ref to pass to `<NavigationContainer ref={…}>`.

* Expo Router

  *use-xerlock-navigation.ts*

  ```ts
  import { init } from '@xerlock/client';
  import { useNavigationContainerRef } from 'expo-router';
  import { useEffect } from 'react';

  export const useXerlockNavigation = () => {
    const navigationRef = useNavigationContainerRef();

    useEffect(() => {
      init({
        navigation: { ref: navigationRef },
      });
    }, [navigationRef]);
  };
  ```

  Show the full example

  Call the hook from your root layout, `app/_layout.tsx`. Xerlock detects Expo Router itself, so there’s nothing else to pass.

The commands appear once the navigation container has mounted.

### Shapes

Xerlock doesn’t depend on React Navigation or Expo Router. The ref only needs to match this shape:

| Member                                                              | Description                                                                         |
| ------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| `isReady(): boolean`                                                | Whether the navigation container has mounted and can handle actions.                |
| `getRootState(): NavigationStateLike \| undefined`                  | Returns the navigation state of the whole tree, or `undefined` before it is ready.  |
| `getCurrentRoute(): { name: string; params?: object } \| undefined` | Returns the focused leaf route.                                                     |
| `navigate(...args: never[]): void`                                  | Navigates to a screen, taking `(name, params)` arguments.                           |
| `dispatch(action: { type: string; payload?: object }): void`        | Dispatches a navigation action such as `{ type: 'POP', payload: { count } }`.       |
| `goBack(): void`                                                    | Goes back in the focused navigator.                                                 |
| `canGoBack(): boolean`                                              | Whether there is a screen to go back to.                                            |
| `resetRoot(state?: object): void`                                   | Replaces the whole navigation state.                                                |
| `addListener(event: 'state', listener: () => void): () => void`     | Calls the listener after every navigation state change, and returns an unsubscribe. |

### Turning it off

Leave `navigation` out, or pass `false`. The navigation commands also need the [commands](/docs/sdk/commands/) plugin, which is on by default.

## Limits and Troubleshooting

* **No navigation commands**: check that `navigation` is set, that the ref comes from `useNavigationContainerRef()` and is passed to your navigation container, and that `init` runs in a `useEffect`. Xerlock ignores a value that isn’t a navigation ref, without an error.
