ReactJS Onsen UI Navigator Component
ReactJS Onsen-UI is a popular front-end library with a set of React components that are designed to developing HTML5 hybrid and mobile web apps in a beautiful and efficient way. Navigator components are responsible for page transitioning and managing the pages of our application. We can use the following approach in ReactJS to use the Onsen-UI Navigator Component.
Navigator Props:
- renderPage: It is a function that takes the current route object as parameter and returns a React component.
- initialRouteStack: It is used to denote an array that contains the initial routes of the navigator.
- initialRoute: It is used to denote an array that contains the initial route of the navigator.
- onPrePush: It is a callback function that is triggered just before a page is pushed.
- onPostPush: It is a callback function that is triggered just after a page is pushed.
- onPrePop: It is a callback function that is triggered just before a page is popped.
- onPostPop: It is a callback function that is triggered just after a page is popped.
- animation: It is used to denote the animations like slide, lift, fade, and none.
- animationOptions: It is used to specify the animation’s duration, delay, and timing.
- swipeable: It is used to enable the swipe-to-pop functionality for iOS.
- swipePop: It is an optional function that is triggered on swipe-to-pop.
- onDeviceBackButton: It is a custom handler for the device back button.
Navigator Methods:
- resetPage(route, options = {}): This method is used to reset the current page. The route parameter denotes the route that the page should be reset to.
- resetPageStack(route, options = {}): This method is used to reset the navigator to the current page stack. The route parameter denotes the routes that the navigator should be reset to.
- pushPage(route, options = {}): This method is used to push a page to the page stack. The route parameter denotes the route that the navigator should push to.
- popPage(options = {}): This method is used to pop a page out of the page stack.
Creating React Application And Installing Module:
-
Step 1: Create a React application using the following command:
npx create-react-app foldername
-
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
-
Step 3: After creating the ReactJS application, Install the required module using the following command:
npm install onsenui react-onsenui
Project Structure: It will look like the following.
Project Structure
Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
App.js
import React from 'react' ;
import 'onsenui/css/onsen-css-components.css' ;
import { Navigator, Button } from 'react-onsenui' ;
export default function App() {
return (
<div style={{
display: 'block' , width: 500, paddingLeft: 30
}}>
<h6>ReactJS Onsen-UI Navigator Component</h6>
<Navigator
renderPage={(route) =>
<Button onClick={() => alert( 'Navigate Accordingly!' )}>
{route.title}</Button>
}
initialRoute={{
title: 'Sample Navigator Button'
}}
/>
</div>
);
}
|
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Reference: https://onsen.io/v2/api/react/Navigator.html
Last Updated :
07 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...