@realmjs/react-stack-navigator
v1.3.2
Published
Simple Stack Navigator for React application
Downloads
6
Readme
React Stack Navigator
React Stack Navigator is a versatile library developed by RealmJS that offers a StackNavigator component, enabling seamless navigation and routing within your React applications. With React Stack Navigator, you can easily define a stack of routes and manage navigation between them, providing a smooth and intuitive user experience.
Installation
npm install @realmjs/react-stack-navigator --save
Example Usage
import StackNavigator, { useStack } from '@realmjs/react-stack-navigator';
const routes = [
['home', (props) => <HomeScreen {...props} />, { path: '/', title: 'Home' } ],
['profile', (props) => <ProfileScreen {...props} />, { path: '/profile/:user', title: 'Profile' } ],
['settings', (props) => <SettingsScreen {...props} />, { path: '/setting', title: 'Settings' } ],
]
const fallbackRoute = ['404', NotFoundScreen]
function App() {
return (
<StackNavigator routeStack={routes} fallback={fallbackRoute} />
)
}
export default App
Leverage the useStack
hook to access the stack API within your components:
const stack = useStack();
Props
The StackNavigator component accepts the following props:
routeStack
(array): An array of route configurations. Each route configuration consists of three elements:[routeId, renderFunction, options]
. TherouteId
is a unique identifier for the route, therenderFunction
is the function that renders the component for the route, andoptions
(optional) is an object that can include apath
and atitle
for the route.fallback
(array): An array representing the fallback route configuration. The fallback configuration consists of three elements:[routeId, renderFunction, options]
. This route will be used when there is no matching route in therouteStack
for the initial URL. TherouteId
is a unique identifier for the fallback route, therenderFunction
is the function that renders the component for the fallback route, andoptions
(optional) is an object that can include apath
and atitle
for the fallback route.onStackReady
(callback): Thestack
handler will be passed to the callback after its ready.
Stack API
The StackNavigator component exposes the following APIs through the useStack
hook:
next(props)
: Moves to the next route in therouteStack
, passingprops
to the render function of the next route.previous(props)
: Moves to the route above the current route in the stack, passingprops
to the render function of the previous route.back(props)
: Returns to the previous route in the navigation history, passingprops
to the render function of the previous route.move(routeId, props)
: Moves to the route with the specifiedrouteId
in therouteStack
. If norouteId
is provided or if therouteId
is not found in therouteStack
, an error will be thrown.props
can be passed to the render function of the destination route.animate(animation, options)
: Configures the animation for the next route transition. Theanimation
parameter specifies the type of animation, whileoptions
(optional) allows for additional customization. This method returns the stack API itself, allowing for method chaining. For example,stack.animate('flyIn 0.4s', { direction: 'right' }).next(props)
.
Note: If you are using React Stack Navigator in a web application, each route in the routeStack
can define a path
in its options. When a route becomes active, the URL will be set to its corresponding path. If the webpage is loaded with a URL that matches one of the route paths, that route will be activated. The path pattern follows the format /path/:params
, where params
can be found as props in the component. For example, if a route has the path /department/:dept/group/:group
, the equivalent props for the render function would be { dept, group }
. When the page is initially loaded with the path /department/it/group/alpha
, the StackNavigator will resolve the props as { dept: 'it', group: 'alpha' }
. Navigating to that route using stack.next({ dept: 'qc', group: 'beta' })
will resolve the path as /department/qc/group/beta
.
License
This project is licensed under the MIT License.