React-Bootstrap is a front-end framework that was designed keeping react in mind. NavBar Component is a navigation header that is responsive and powerful. It supports navigation, branding, and many more other related features. We can use the following approach in ReactJS to use the react-bootstrap NavBar Component.
NavBar Props:
- as: It can be used as a custom element type for this component.
- bg: It is used to add bg-* utility classes.
- collapseOnSelect: It is used to collapse the Navbar on click onSelect function is triggered of a descendant of a child <nav> element.
- expand: It is used to denote the breakpoint below which our Navbar will collapse.
- expanded: The visibility of the navbar body is controlled through it.
- fixed: It is used to create a fixed navbar that stays along the bottom of the screen or top of the screen.
- onSelect: It is a callback that is triggered when a descendant of a child <nav> is selected.
- onToggle: A callback fired when the <Navbar> body collapses or expands
- role: It is used to define the ARIA role for the navbar.
- sticky: It is used to position our given navbar at the viewport’s top.
- variant: It indicates the visual variant of the navbar.
- bsPrefix: It is an escape hatch for working with strongly customized bootstrap CSS.
Navbar.Brand Props:
- as: It can be used as a custom element type for this component.
- href: It is used to pass the href attribute to this element.
- bsPrefix: It is an escape hatch for working with strongly customized bootstrap CSS.
Navbar.Toggle Props:
- as: It can be used as a custom element type for this component.
- children: It is used to define the toggle content.
- label: For the toggler button, it is used as an accessible ARIA label.
- onClick: It is the callback function that is triggered on click event.
- bsPrefix: It is an escape hatch for working with strongly customized bootstrap CSS.
Navbar.Collapse Props:
- bsPrefix: It is an escape hatch for working with strongly customized bootstrap CSS.
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 react-bootstrap npm install bootstrap
Project Structure: It will look like the following.
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 'bootstrap/dist/css/bootstrap.css' ; import Navbar from 'react-bootstrap/Navbar' ; export default function App() { return ( <div style={{ display: 'block' , width: 700, padding: 30 }}> <h4>React-Bootstrap NavBar Component</h4> <Navbar bg= "dark" variant= "dark" > <Navbar.Brand href= "#home" > <img alt= "Sample Brand Logo" width= "35" className= "align-top d-inline-block" height= "35" />Test Company </Navbar.Brand> </Navbar> </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://react-bootstrap.github.io/components/navbar/