Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Bootstrap 4 with React

Sorry for my lack of knowledge but I'm new and currently learning React. I just wanted to ask if I wanted to use Bootstrap 4 with my React app, do I have to install jQuery? I read somewhere that using jQuery with React is a NO-NO. So now I'm wondering. Thanks for the reply. Your advice and suggestions are truly appreciated.

like image 385
Sonson Ixon Avatar asked Dec 27 '18 07:12

Sonson Ixon


People also ask

Can I use Bootstrap 4 in React?

You can also import Bootstrap in your React application by installing it from npm . After installing the bootstrap package, you will need to import it in your React app entry file.

Can Bootstrap and React be used together?

Bootstrap can be used directly on elements and components in your React app by applying the built-in classes as you would any other class. To demonstrate the use of Bootstrap classes and components, let's create a basic theme switcher React component.

Can I use Bootstrap and React Bootstrap together?

Install React Bootstrap Package The other method to add bootstrap in your React component is adding a package with the inbuilt bootstrap component. These are designed to work with your React application components. Below is the name of two popular packages. Both are good choices for using Bootstrap with React apps.


3 Answers

Certain functionalities such as dropdown, modal requires JS to manipulate the DOM, and bootstrap uses jQuery to handle the DOM manipulations.

However, React uses virtual DOM, so manipulating the browser DOM outside your React app through jQuery means React is potentially no longer handling state, events and UI rendering. And React broadly expects/assumes that nothing else will be modifying the DOM.


This is why react-bootstrap or reactstrap are recommended. The CSS remains exactly the same, but the components that initially require jQuery are rewritten.

Take this bootstrap 4 modal as example, you need to define modal state which determines whether the modal is being shown or hidden.

So essentially these react bootstrap libraries rewrite each bootstrap component into a React component, CSS wise it's entirely the same.

like image 176
Liren Yeo Avatar answered Oct 11 '22 23:10

Liren Yeo


Update 2020 - Bootstrap 5

Bootstrap 5 no longer requires jQuery and is modular so you can @import it like any other JS module which makes it easier to use with React. It also means you can use Bootstrap without a 3rd party library like reactstrap or react-bootstrap.

Also, you can import components and reference them directly. For example:

const popover = new Popover(document.querySelector('#myButton'), options)

This makes it easier to use Bootstrap 5 with the React 16.8+ useEffect and useState hooks. So that we avoid direct DOM manipulation in React, get the element instance with a useRef hook...

function PopoverDemo() {
  const popoverRef = useRef()
  useEffect(() => {
    var popover = new Popover(popoverRef.current, {
        content: "Hello popover content!",
        title: "My Popover",
        trigger: 'hover'
    })
  })

  return (
    <div className="py-2">
        <button className="btn btn-danger" ref={popoverRef}>
            Hover for popover
        </button>
    </div>
  )
}

Bootstrap 5 with React Demo

like image 22
Zim Avatar answered Oct 12 '22 00:10

Zim


You should be looking to use react-bootstrap. Instead, you can install it using npm: $npm install --save react-bootstrap Find more about it on this link

just remember that it uses the Bootstrap v3.

Happy coding!

like image 40
Jean Rauwers Avatar answered Oct 12 '22 00:10

Jean Rauwers