44

Does anyone know if you can use react-bootstrap with Next.js? I am using the latest versions of both, I can provide code at this point, but right now my app is not throwing any errors, it is just not registering any of my react-bootstrap components. Maybe there is some trick that I have not been able to find on the internet? I can provide code or file structure if that helps. Thanks in advance!

next.config.js

const withCSS = require('@zeit/next-css')

module.exports = withCSS({
    /* my next config */
})

pages/index.js

import Badge from 'react-bootstrap/Badge';
import "../public/css/bootstrap.css";
const Index = () => (
    <div>
        <p className='display-4'>Hello</p>
        <Badge>Heading</Badge>
    </div>
)

export default Index;

package.json

{
  "name": "vacation-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@zeit/next-css": "^1.0.1",
    "next": "^9.1.1",
    "react": "^16.10.2",
    "react-bootstrap": "^1.0.0-beta.14",
    "react-dom": "^16.10.2"
  }
}
7
  • 1
    Yes. You should provide your minimal, runnable code structure. Commented Oct 22, 2019 at 2:32
  • using the className "display-4" works, meaning the boostrap that I downloaded works on its own. But react-bootstrap does not register Commented Oct 22, 2019 at 2:37
  • however, using the Badge component imported from react-bootstrap, I just noticed that using chrome dev tools, it registers the Badge component as a span element with a class of "badge"... Does that mean react-bootstrap is semi-working? In the browser, it doesn't show any sort of formatting, just the text in the inner HTML Commented Oct 22, 2019 at 2:40
  • If it is giving you the span in dev tools, then your react-bootstrap is working fine. You just need to provide variant="primary" like <Badge variant="primary">Heading</Badge>. Read more about variant Commented Oct 22, 2019 at 2:42
  • I had already tried that, it is not showing any formatting in browser. I have tried other components too. Thanks for your quick responses by the way friend Commented Oct 22, 2019 at 2:43

7 Answers 7

65

Yes, it is possible to use react-bootstrap in a nextjs application.

One of the problems you might encounter will be in the rendering of your application if javascript is disabled in the user's browser and you use react-bootstrap components to build your layout (see example below).

Nextjs allows you to display SSG/SSR pages, users without javascript can see your application but the layout might be messy.

But if you still want to go with it:

npm i react-bootstrap bootstrap

Import bootstrap styles in your _app.js:

import 'bootstrap/dist/css/bootstrap.min.css';

You can then use your react-bootstrap components as you would do in reactjs:

import {Container, Row, Col} from 'react-bootstrap';
        
const Layout = () => (
  <>
    <Container fluid>
      <Row>
        <Col>
          <p>Yay, it's fluid!</p>
        </Col>
      </Row>
    </Container>
  </>
);
        
export default Layout;
Sign up to request clarification or add additional context in comments.

Comments

25

Yes, I am using it!
You can follow egdavid's answer to configure react-bootstrap.

The only issue I found is that links are not working properly in terms of SPA.
For that, I found the below solution. You have to wrap NavBar.Link within Link and use passHref attribute if you want to visible hyperlink on the link.

<Link href="/" passHref>
    <Nav.Link>Home</Nav.Link>
</Link>
<Link href="/contact" passHref>
    <Nav.Link>Contact</Nav.Link>
</Link>

Comments

11

Yes you can use it. I think the most important issue is that server-side rendering supported in combination of next js & react-bootstrap? yes in react-bootstrap docs you can see how to implement this option.

https://react-bootstrap.github.io/docs/getting-started/server-side-rendering

you could do it something like this:

import SSRProvider from 'react-bootstrap/SSRProvider';

   <SSRProvider>
     <App />
   </SSRProvider>

Comments

3

yes , you can use bootstrap and react-bootstrap inside next.js.

and i am use it inside my applications ,

and here is a simple example to use navbar and modal component just for testing ..

1- first install "react-bootstrap bootstrap" by using the follwing commands :

npm install react-bootstrap bootstrap

2- inside "_app.js" file import bootstrap css file on the top of file , and bootstrap js file inside useEffect , like the following :

import { useEffect } from 'react';
import Nav from '../components/nav'
import 'bootstrap/dist/css/bootstrap.css';


function MyApp({ Component, pageProps }) {
    
    useEffect(() => {
        require ('bootstrap/dist/js/bootstrap.js')
    }, []);

  return (
    <>
        <Nav />
        <Component {...pageProps} />  
    </>
 
  )
}

3- and here is the my Nav component for testing (compatible with bootstrap 5) ... :

import { useState } from "react";
import {Modal, Button} from 'react-bootstrap';

const Nav = () => {
    const [show, setShow] = useState(false);
    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);

    const confirm_modal = () => {
        console.log('modal is working ...')
        setShow(false)
    }

    return(
        <>

<nav className="navbar navbar-expand-lg navbar-light bg-light">
  <div className="container-fluid">
    <a className="navbar-brand" href="#">Navbar</a>
    <button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span className="navbar-toggler-icon"></span>
    </button>
    <div className="collapse navbar-collapse" id="navbarSupportedContent">
      <ul className="navbar-nav me-auto mb-2 mb-lg-0">

        <li className="nav-item">
          <a className="nav-link" onClick={handleShow} href="#"> test modal</a>
        </li>
        <li className="nav-item dropdown">
          <a className="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown
          </a>
          <ul className="dropdown-menu" aria-labelledby="navbarDropdown">
            <li><a className="dropdown-item" href="#">Action</a></li>
            <li><a className="dropdown-item" href="#">Another action</a></li>
            <li><hr className="dropdown-divider" /></li>
            <li><a className="dropdown-item" href="#">Something else here</a></li>
          </ul>
        </li>

      </ul>
      <form className="d-flex">
        <input className="form-control me-2" type="search" placeholder="Search" aria-label="Search" />
        <button className="btn btn-outline-success" type="submit">Search</button>
      </form>
    </div>
  </div>
</nav>



      <Modal show={show} onHide={handleClose}  keyboard="false">
        <Modal.Header closeButton>
          <Modal.Title>Modal heading</Modal.Title>
        </Modal.Header>
        <Modal.Body>Are You Sure You Want This!.</Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={handleClose}>
            Close
          </Button>
          <Button variant="danger" onClick={ confirm_modal  }>
          Yes
          </Button>
        </Modal.Footer>
      </Modal>
 
        </>
    )
}

export default Nav

4- now create any page (for example index.js) and test it .


how to test :

1- if you see bootstrap styles showing on navbar (colors , Buttons styles ) that's means the 'boostrap css is working fine'.

2- if the dropdown menue is clickable and working , that's means bootstrap js file is working fine .

3- if the test modal works and you see the confirmation box , that's means the react-bootstrap components works fine .

this is just example and change it based on your case/requirements .

i hope this helpful for you .

Comments

1

I presume we can add an update here.

It's always ok to use react-bootstrap inside a NextJS application, even with the last NextJs 13.4.x update, which added the new app directory.

But now, in order to keep the app running, every single component you are using in your application, if you decide to move to this latest update, and use the app directory, you have to use the directive "use client" at the top of your component if your are importing and using react-bootstrap component

react component example:

import { Container } from 'react-bootstrap';

const myReactComponentWithRB: React.FC<{}> = ({}) => {
   return (
       <Container>
         <h1>This is a test</h1>
       </Container>
   );
});

export default myReactComponentWithRB;

Normaly, there is no problem with this code. But if your are importing this component with the new app dir, you will need to add, "use client" at the top of your file, OR, you can add it at the top of the file which include this component. But if you do that, the component will not be rendered by your Next server, but by the browser.

2 Comments

Thanks for this update. I found it very useful. It would be pretty pointless installing react-bootstrap if you are not using it for all your JSX components. This means you'd be sacrificing the entire server-side aspect of Next.js - which is its default. This issue screams out for the need of a nextjs-bootstrap fork of the react-bootstrap library which is "use client" compatible.
I presume we have to wait for that, because react-bootstrap has to migrate all their components to react 18 version (which is using server components), but for now, you have to use "use client" directive, pretty frustrating I admit
0

Yup, you can use it, by providing CDN link inside your <head> tag in pages/index.js.

import Head from 'next/head';
import Badge from 'react-bootstrap/Badge';
import "../public/css/bootstrap.css";
const Index = () => (
    <Head>
      <link
        rel="stylesheet"
        href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
        crossorigin="anonymous"
      />
    </Head>
    <div>
        <p className='display-4'>Hello</p>
        <Badge>Heading</Badge>
    </div>
)

export default Index;

Hope it works!!!

2 Comments

So is it impossible to use bootstrap with next without using cdn
You can download the css file and hosts it in your asset file. you can also do this import 'bootstrap/dist/css/bootstrap.min.css'; in your _app.js file like in the example above
0

For Next JS 14 App Directory. First, install react-bootstrap

npm install react-bootstrap bootstrap

Then go to layout.jsx or layout.tsx file and import bootstrap CSS

import 'bootstrap/dist/css/bootstrap.min.css'

Here is the layout.tsx full code

import './globals.css'
import { Inter } from 'next/font/google'
import 'bootstrap/dist/css/bootstrap.min.css'

const inter = Inter({ subsets: ['latin'] })

export const metadata = {
  title: 'CodingMSTR',
  description: 'CodingMSTR',
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body className={inter.className}>{children}</body>
    </html>
  )
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.