Your experience on this site will be improved by allowing cookies
React Bootstrap
Bootstrap is one of the most popular CSS frameworks. Bootstrap can be added to the React app in mainly three ways.
Being the easiest way of adding Bootstrap, there is no need to install or download Bootstrap while using the Bootstrap CDN for React app. It can be simply added with the below links.
To include jQuery, Popper.js, and Bootstrap.js in the document, the following imports needs to be added in the tag of the index.html file. Here, jQuery's slim version is used, however we can also use the full version.
$npm install bootstrap --save
import 'bootstrap/dist/css/bootstrap.min.css';
$ npm install jquery popper.js
import $ from 'jquery'; import Popper from 'popper.js'; import 'bootstrap/dist/js/bootstrap.bundle.min';
It is the most popular way to add bootstrap in the React application. There are mainly two popular Bootstrap packages offered by the community that is mostly utilised.
react-bootstrap:
Without any dependencies, React-Bootstrap offers everything that the user needs, along with the React setup. It can simply be understood as a complete re-implementation of the Bootstrap components as React components
reactstrap:
It is a library that does not depend on jQuery or Bootstrap JavaScript, as it contains React Bootstrap 4 components that favour composition and control.
React Bootstrap Installation:
To create a new React app:
Command:
$ npx create-react-app react-bootstrap-app
Now navigate to the React app folder. Next, to install Bootstrap:
Command:
$ npm install react-bootstrap bootstrap --save
Importing Bootstrap:
To import the Bootstrap file, add the following code to the src/index.js file.
import 'bootstrap/dist/css/bootstrap.min.css';
Instead of the entire library, individual components can also be imported from 'react-bootstrap'. Now, create a new file named ThemeSwitcher.js.
ThemeSwitcher.js:
import React, { Component } from 'react'; import { SplitButton, Dropdown } from 'react-bootstrap'; class ThemeSwitcher extends Component { state = { theme: null } chooseTheme = (theme, evt) => { evt.preventDefault(); if (theme.toLowerCase() === 'reset') { theme = null } this.setState({ theme }); } render() { const { theme } = this.state; const themeClass = theme ? theme.toLowerCase() : 'default'; const parentContainerStyles = { position: 'absolute', height: '100%', width: '100%', display: 'table' }; const subContainerStyles = { position: 'relative', height: '100%', width: '100%', display: 'table-cell', }; return ( {theme || 'Default'} ); } } export default ThemeSwitcher; Theme1 Theme2 Theme3 Theme0 |
Update the src/index.js file.
Index.js:
import 'bootstrap/dist/css/bootstrap.min.css'; import React from 'react'; import ReactDOM from 'react-dom'; import App from './App.js'; import './index.css'; import ThemeSwitcher from './ThemeSwitcher'; ReactDOM.render(, document.getElementById('root')); |
Using reactstrap:
To create a new React app:
Command:
$ npx create-react-app reactstrap-app
Navigate to the React app folder, and install the reactstrap via the npm package.
Command:
$ npm install bootstrap reactstrap --save
Importing Bootstrap:
To import the Bootstrap file, add the following code to the src/index.js file.
import 'bootstrap/dist/css/bootstrap.min.css';
Instead of the entire library, individual components can also be imported from 'react-bootstrap'. Now, create a new file named ThemeSwitcher.js.
ThemeSwitcher.js:
import React, { Component } from 'react'; import { Button, ButtonDropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap'; class ThemeSwitcher extends Component { state = { theme: null, dropdownOpen: false } toggleDropdown = () => { this.setState({ dropdownOpen: !this.state.dropdownOpen }); } resetTheme = evt => { evt.preventDefault(); this.setState({ theme: null }); } chooseTheme = (theme, evt) => { evt.preventDefault(); this.setState({ theme }); } render() { const { theme, dropdownOpen } = this.state; const themeClass = theme ? theme.toLowerCase() : 'secondary'; return ( {theme || 'Default'} {theme || 'Main'} Theme this.chooseTheme('One', e)}>Theme1 this.chooseTheme('Two', e)}>Theme2 this.chooseTheme('Three', e)}>Theme3 Theme0 ); } } export default ThemeSwitcher; |
Update the src/index.js file.
Index.js:
import 'bootstrap/dist/css/bootstrap.min.css'; import React from 'react'; import ReactDOM from 'react-dom'; import App from './App.js'; import './index.css'; import ThemeSwitcher from './ThemeSwitcher'; ReactDOM.render(, document.getElementById('root')); |
0 comments