I have some designs in html which uses bootstrap and I try to make them dynamic using nextjs.
When I convert html to tsx (basically renaming class attributes to className) there doesnt seem to be any problem untill I add bootstrap which requires jquery. I also include jquery but bootstrap cannot find jquery in nextjs context for some reason.
Things to run
Hello! Questions are reserved for our Spectrum community: https://spectrum.chat/next-js.
Please repost this there!
this is a bug not a question @Timer . I already asked it on the spectrum but apparently there's no solution or workaround. People only suggested leaving bootstrap and using som other libraries such as reactstrap.
Is bootstrap simply not supported by nextjs?
You should never use jquery
and bootstrap
's JavaScript in a React application. This is true for all React apps鈥攏ot only Next.js.
Using a library like React Bootstrap gives you minimal bundle size and full React support.
Mixing Bootstrap JS (+ jQuery) will always result in application bugs because you're mutating elements that React controls.
If you must use Bootstrap's JS and jQuery, note that these are client-side only libraries and must be loaded as so:
import "bootstrap/dist/css/bootstrap.css";
import { useEffect } from "react";
export default function MyApp({ Component, pageProps }) {
useEffect(() => {
import("jquery").then($ => {
// jQuery must be installed to the `window`:
window.$ = window.jQuery = $;
return import("bootstrap");
});
}, []);
return <Component {...pageProps} />;
}
I highly advise you do not use this in your application. Use React Bootstrap instead!
thanks
Feel free to share this answer on the question you posted!
@Timer the very documentation tells you to import bootstrap css from the node_modules folder.
Install bootstrap with a package manager npm install bootstrap
or yarn add bootstrap
Import it inside your _app.js
import 'bootstrap/dist/css/bootstrap.min.css'
This worked for me.
Most helpful comment
You should never use
jquery
andbootstrap
's JavaScript in a React application. This is true for all React apps鈥攏ot only Next.js.Using a library like React Bootstrap gives you minimal bundle size and full React support.
Mixing Bootstrap JS (+ jQuery) will always result in application bugs because you're mutating elements that React controls.
If you must use Bootstrap's JS and jQuery, note that these are client-side only libraries and must be loaded as so:
I highly advise you do not use this in your application. Use React Bootstrap instead!