react-bootstrap navbar nav link refreshes page
To prevent the page from refreshing when clicking on a react-bootstrap
navbar nav link, you can make use of the react-router-dom
library. Here are the steps to achieve this:
- Install the necessary packages by running the following command in your project directory:
npm install react-router-dom
- Import the required components from
react-router-dom
in your Navbar component file:
import { Link } from 'react-router-dom';
- Replace the
href
attribute of the<Nav.Link>
component with theto
attribute fromreact-router-dom
. For example:
<Nav.Link as={Link} to="/your-route">Link Text</Nav.Link>
- Wrap your entire application with the
<BrowserRouter>
component fromreact-router-dom
in your main file (usuallyindex.js
):
import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
);
That's it! By following these steps, you should be able to use react-router-dom
to navigate within your application without causing the page to refresh when clicking on the navbar links.