use bootstrap 5 in react
Tags: javascript, react, bootstrap
Published: 2021-02-25 22:34:42

In following [use bootstrap 5 in vue correctly](https://dev.to/tefoh/use-bootstrap-5-in-vue-correctly-2k2g) post here we will see how use bootstrap in react application. because bootstrap 5 drop support jQuery we can easily use it without an ui framework like React Bootstrap. first we load bootstrap styles and then we use bootstrap modal to see how bootstrap javascript actually works.
## install react with CRA
first we install react application with create react app. to install react app run this command:
```
npx create-react-app react-bootstrap
```
then go project directory, install dependencies and run the CRA server.
## install bootstrap 5
now the react app installed we should install bootstrap and node-sass for compile scss files. run this commands:
```
yarn add bootstrap@next node-sass
```
now we can load bootstrap styles. in `index.js` file we can easily load bootstrap.scss like this:
```javascript
import 'bootstrap/scss/bootstrap.scss'
```
if we use bootstrap classes for e.x. btn classes like `<button className="btn btn-primary">test</button>` we will see styles applied.
## use bootstrap modal
bootstrap besides the styles give us some javascript that make our life easier and modal is one simple example. for this we will use bootstrap docs modal example. you can use this in any component template.
```react
{/* show btn Modal */}
<button type="button" className="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
  Launch demo modal
</button>

{/* Modal */}
<div className="modal fade" id="exampleModal" tabIndex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div className="modal-dialog">
    <div className="modal-content">
      <div className="modal-header">
        <h5 className="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div className="modal-body">
        ...
      </div>
      <div className="modal-footer">
        <button type="button" className="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" className="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
```
there is two parts here one is the button show the modal and second part is the modal. so if we use this nothing works, why? because we just load styles not bootstrap javacript. so we load it too. with `import { Modal } from 'bootstrap'` we can load only the bootstrap modal javascript part, awesome. but we will get an error that says @popperjs/core package not installed. lets install it with this simple command:
```
yarn add @popperjs/core
```
bootstrap need this package for javascript part. now modal should works. nice :) but we havent use react vDOM too make this modal work lets do it with vDOM.
## use virtual dom
react and vue use virual dom and because of that we use mostly with state in this frameworks but when we need to use dom elements not state like when want to focus input we should use refs. there is two ways to use bootstrap javascript one using data-bs, two use javascript. with second way we can use refs to make modal works. bootstrap docs says we can use it like this:
```javascript
var myModal = new Modal(document.getElementById('exampleModal'), options)
```
Modal is the one we imported earlier. we can replace document.getElementById('exampleModal') with our ref and we will name our ref exampleModal. we will need a state for modal to show and hide the modal. and for that we will use react hooks:
```react
const [modal, setModal] = useState(null)
const exampleModal = useRef()

useEffect(() => {
  setModal(
    new Modal(exampleModal.current)
  )
}, [])
```
with modal.show() we can show modal and for hide just use hide method and the button for show modal will be like this:
```react
<button type="button" onClick={() => modal.show()} className="btn btn-primary">
  Launch demo modal
</button>
```
and now we use vDOM to use bootstrap modal. good job :)) the hole simple component will be like this:
```react
import { Modal } from 'bootstrap'
import { useState, useEffect, useRef } from 'react'

function App() {
  const [modal, setModal] = useState(null)
  const exampleModal = useRef()

  useEffect(() => {
    setModal(
      new Modal(exampleModal.current)
    )
  }, [])

  return (
    <>
      {/* show btn Modal */}
      <button type="button" onClick={() => modal.show()} className="btn btn-primary">
        Launch demo modal
      </button>

      {/* Modal */}
      <div className="modal fade" ref={exampleModal} tabIndex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div className="modal-dialog">
          <div className="modal-content">
            <div className="modal-header">
              <h5 className="modal-title" id="exampleModalLabel">Modal title</h5>
              <button type="button" className="btn-close" onClick={() => modal.hide()} aria-label="Close"></button>
            </div>
            <div className="modal-body">
              ...
            </div>
            <div className="modal-footer">
              <button type="button" className="btn btn-secondary" onClick={() => modal.hide()}>Close</button>
              <button type="button" className="btn btn-primary">Save changes</button>
            </div>
          </div>
        </div>
      </div>
    </>
  );
}

export default App;
```
## Summary
we created a react application and installed bootstrap. then we used bootstrap modal with data-bs and easy way but after that used react vDOM and correct way. i hope this short post helped, i covered this topics in my course and shared my little Knowledge about this topic.