Skip to content
English

Navigation

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

Accepts false | NavigationPluginConfig Opt-in
NavigationPluginConfig
OptionTypeDefaultDescription
ref requiredNavigationContainerRefLike—The container ref from useNavigationContainerRef(), from React Navigation or Expo Router. Call init in a useEffect once you have it.
use-xerlock-navigation.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;
};

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

The commands appear once the navigation container has mounted.

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

NavigationContainerRefLike
MemberDescription
isReady(): booleanWhether the navigation container has mounted and can handle actions.
getRootState(): NavigationStateLike | undefinedReturns the navigation state of the whole tree, or undefined before it is ready.
getCurrentRoute(): { name: string; params?: object } | undefinedReturns the focused leaf route.
navigate(...args: never[]): voidNavigates to a screen, taking (name, params) arguments.
dispatch(action: { type: string; payload?: object }): voidDispatches a navigation action such as { type: 'POP', payload: { count } }.
goBack(): voidGoes back in the focused navigator.
canGoBack(): booleanWhether there is a screen to go back to.
resetRoot(state?: object): voidReplaces the whole navigation state.
addListener(event: 'state', listener: () => void): () => voidCalls the listener after every navigation state change, and returns an unsubscribe.

Leave navigation out, or pass false. The navigation commands also need the commands plugin, which is on by default.

  • 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.