How to use the new Bootstrap 5 with React
Bootstrap and React have both been around for a while now, but now that the Bootstrap 5 beta i...
For further actions, you may consider blocking this person and/or reporting abuse
Aren't we supposed to avoid direct interaction with the DOM in a React app? Your useState targets the element directly by name; and also directly adds an event listener. Is there another way to make use of Bootstrap without the 3rd party libraries?
This is a great question and you're absolutely right! As explained in the React hooks FAQ you'd want
useRef
in this case. This will ensure we get the.current
instance of the DOM element that's synched with the React lifecycle. I've updated the Toast example accordingly...I also updated the other examples to follow this same pattern.
Thank you for the article, I have checked the examples and still can't be sure if it is okay to use bs components with only data-bs attribute.
Is it a bad practice for React virtual dom to use it like below.
For some or maybe most of these examples you should probably also add an empty array for the useEffect dependencies. Basically saying that we only want to run this code once (to create the component), then it does not have to run again as long as the component is left in the DOM tree.
But then the
bsToast.show()
orbsToast.hide()
wouldn't trigger when the state oftoast
changes. In my case I only added[toast]
to the useEffect dependencies.why the error 'bootstrap' is not defined appear? the bootstrap.Toast give error. where the bootstrap comes from?
I did something similar with the Modal component. But I also had to include an event listener when the user clicks outside of the modal to close it:
Please Provide an example for the Dropdown Component.
It works just like the others. See: codeply.com/p/IdzoX6osI2
Thank you.
Great explaination and example links ⭐️
Yes, as Joel rightly pointed out what you have done - is it the right way to do things in React?
Nice tutorial, thanks.
For those who are looking for ready to use solutions - you can also try out this Bootstrap 5 & React free UI KIT by MDB
mdbootstrap.com/docs/b5/react/
For those who are struggling with the navbar and the toggler, here is a good example. dev.to/johnotu/how-to-toggle-boots...
So does this make Reactstrap and React Bootstrap obsolete?
I don't think so, it just makes it possible to use Bootstrap in React in a way that aligns with React patterns/practices.