Navigation adds commands to the 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
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. |
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.
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;};The hook returns the ref to pass to <NavigationContainer ref={…}>.
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]);};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
Section titled “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
Section titled “Turning it off”Leave navigation out, or pass false. The navigation commands also need the
commands plugin, which is on by default.
Limits and Troubleshooting
Section titled “Limits and Troubleshooting”- No navigation commands: check that
navigationis set, that the ref comes fromuseNavigationContainerRef()and is passed to your navigation container, and thatinitruns in auseEffect. Xerlock ignores a value that isn’t a navigation ref, without an error.