HI WELCOME TO SIRIS

Integrating Bootstrap with React

Leave a Comment
React is a JavaScript-based library which is used to create the interactive user interface. It follows the Virtual DOM concept in which view will be updated efficiently whenever your application’s state changed.Few advantages to using react
  • Declarative
  • Component-based architecture
  • Learn once, write anywhere kind of features
  • Compare to a framework like Angular, in React we don’t have any pre-defined structure, so that in recatjs we can have multiple options to achieve responsiveness of an application.In this article, we are going to use Bootstrap with React application to use different component/parts of bootstrap to achieve responsiveness.For that, we need to create new react application, and in order to create new react app we need to install react into our machine by using following npm command.

Prerequisites

  • Node 6.0 > installed
  • Npm(windows) 5.2 > or Nvm(Linux/macOS)
  • Any standard code editor like Visual Studio Code, Visual Studio etc.
  • To install react to your machine, we need to run following npm command.
    Npm install create-react-app
    After executing above npm command, now we will be able to use and create new react application, let’s start by using bootstrap into our react application, to get started we need to create a new application by using following npm command.
    create-react-app reactwithbootstrap
    After running the above command, our react application will be created and you can see the complete information in the power shell like this.
Now open the project and then you can see the following project structure.
When we create a new application at a time many important dependencies are installed by default like react, react-dom, react-scripts etc.Now let’s start using bootstrap by using bootstrap CDN, in order to use CDN in react app, open public/index.html and add CDN like this.
Public/index.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  6. <meta name="theme-color" content="#000000">
  7. <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
  8. <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
  9. <!-- Bootstrap CDN -->
  10. <link rel="stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  11. <title>React App</title>
  12. </head>
  13. <body>
  14. <noscript>
  15. You need to enable JavaScript to run this app.
  16. </noscript>
  17. <div id="root"></div>
  18. </body>
  19. </html>

Vanilla Bootstrap with React

Let me tell you that what is Vanilla bootstrap, it just a term which defines that we are using the only bootstrap without using any additional extensions or plug-in. So far we have created new react application, and then added bootstrap CDN into an index.html file, now we should try layout of the bootstrap grid into our app, now let’s see a simple example using bootstrap component.
Open App.js and replace the following code.
  1. import React, { Component } from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4.  
  5. class App extends Component {
  6. render() {
  7. return (
  8. <div className="App">
  9. <div class="container">
  10. <div class="row">
  11. <div class="col-sm">
  12. One of three columns
  13. </div>
  14. <div class="col-sm">
  15. One of three columns
  16. </div>
  17. <div class="col-sm">
  18. One of three columns
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. );
  24. }
  25. }
  26.  
  27. export default App;
And I have added one class for columns, to add open index.css and add the following style sheet.
Index.css
  1. .col-sm
  2. {
  3. background-color: yellow;
  4. height: 100px;
  5. }
Now let’s try to run our react application and see the output.
This is how we have used a simple grid system without using an additional plug-in which is called vanilla bootstrap, but in this case if we want to use other functionality like navigation bar, toggle panel etc so for that we need to have jquery to be used in our app, by just using vanilla bootstrap we cannot achieve so many functionalities stated above, so we can make use of many another third-party library with react.

Third-party Libraries for React and Bootstrap

As we know that React is JavaScript based library so we have the choice to use structure our application and also can use a third-party library. There are tons of libraries are available to use with React to implement the Bootstrap module in our application, many of them are listed below.
  • Reactstrap
  • React-bootstrap
  • Grommet
  • Blueprint
  • Ant design react
  • Semantic UI React
  • And many more libraries are available, but in this article, we are going to cover two of them I mean Reactstrap and a React-bootstrap.

Reactstrap

Reactstrap is one of the most used with the 50k+ download on weekly basis, mainly reactstrap is used to include and use Bootstrap in our react application.Reactstrap has a large number of components to use with web application and few components are listed below.
  • Card
  • Carousel
  • Form
  • Breadcrumbs
  • Modals
  • Navbar
  • Tables
  • Tabs
  • And many other useful components are included into Reactstrap, let’s implement two of them into react application, but before we need to install reactstrap in our project, for that execute below npm command.
Npm install reactstrap
After executing the above command, you can see that the reactstrap package will be installed.
Now we have installed reactstrap, our next step is to create new folder named reactstrap-components and inside the folder create new file named Table.js and paste following code.
/reactstrap-compoents/Table.js
  1. import React, { Component } from 'react';
  2. import { Table } from 'reactstrap';
  3.  
  4. class ReactTable extends Component {
  5. render() {
  6. return (
  7. <Table>
  8. <thead>
  9. <tr>
  10. <th>ID</th>
  11. <th>Employee Name</th>
  12. <th>Department</th>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. <tr>
  17. <th scope="row">1</th>
  18. <td>Manav Pandya</td>
  19. <td>Information Technology</td>
  20. </tr>
  21. <tr>
  22. <th scope="row">2</th>
  23. <td>C#Corner</td>
  24. <td>Management</td>
  25. </tr>
  26. </tbody>
  27. </Table>
  28.  
  29. );
  30. }
  31. }
  32.  
  33. export default ReactTable;
In above code snippet of Table.js, I have imported Table from reactstrap package which contains functionality for a bootstrap table, and in the code snippet, I have created a table and provided the structure same as we used for simple HTML table, now let’s import it into app component as described below.
App.js
Here in App.js file, we need to import the file which was previously created by us named Table.js and use it into App component, following code snippet is for App.js.
  1. import React, { Component } from 'react';
  2. import './App.css';
  3. import ReactTable from './reactstrap-components/Table';
  4.  
  5. class App extends Component {
  6. render() {
  7. return (
  8. <div className="App">
  9. <ReactTable/>
  10. </div>
  11. );
  12. }
  13. }
  14.  
  15. export default App;
Here in this file, we have used <ReactTable/> markup tag which is used to include an external file as a reusable component and can include conditionally wherever we want in our react application. By executing our project we will get output like this.
This is how we have developed a reusable component for a bootstrap table, same way let’s create a new File named Card.js to use Card Component in our application.
/reactstrap-components/Card.js
In order to use bootstrap card component, we need to import card component from reactstrap and paste following code snippet inside the newly created file
  1. .
  2. import React, { Component } from 'react';
  3. import {
  4. Card, CardImg, CardText, CardBody,
  5. CardTitle, CardSubtitle, Button
  6. } from 'reactstrap';
  7.  
  8. class ReactCard extends Component {
  9. render() {
  10. return (
  11. <div>
  12. <Card>
  13. <CardImg top width="100%" src="https://dummyimage.com/318x180/000/fff" alt="Image Not Found" />
  14. <CardBody>
  15. <CardTitle>This is card title</CardTitle>
  16. <CardSubtitle>This is card subtitle</CardSubtitle>
  17. <CardText>This is the card text section where you can use different bootstrap element</CardText>
  18. <Button>Click Me</Button>
  19. </CardBody>
  20. </Card>
  21. </div>
  22. );
  23. }
  24. }
  25.  
  26. export default ReactCard;
Now open app.js, here we are going to import the Card component that we have created just before, and that uses its markup tag in order to render the bootstrap component.
App.js
  1. import React, { Component } from 'react';
  2. import './App.css';
  3. import ReactCard from './reactstrap-components/Card';
  4.  
  5. class App extends Component {
  6. render() {
  7. return (
  8. <div className="App">
  9. <ReactCard/>
  10. </div>
  11. );
  12. }
  13. }
  14.  
  15. export default App;
Now it’s time to run above example, and you can see the working example will look like this.
So far we have learned that what is the react strap, how to install it and use different bootstrap component within our react application, you can try other different components as well.

React-bootstrap

React-bootstrap is a powerful front-end framework which is used to create a responsive site using react library; react-bootstrap is a rebuilt version of the bootstrap framework. Currently, react-bootstrap supports v3 instead of v4, so you can use many components which are included in version 3 of bootstrap.In order to use react-bootstrap with react, we need to install it using following npm command.
Npm install react-bootstrap
After successful installation of a package, in console you can see like this
Apart from that we need to add additional theme css file into /public/index.html file like this.
  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
  2. crossorigin="anonymous">
Now we can use many components of react-bootstrap like.
  • Carousel
  • Jumbotron
  • Modal
  • Table
  • Breadcrumbs
  • Navbar
  • Forms
  • And many more components are included within react-bootstrap, in this tutorial I am going to use two different react-bootstrap components.
  • Jumbotron
  • Carousel
  • For that create one new folder under src folder named react-bootstrap and create two different js files like this.
  • Jumbotron.js
  • Carousel.js
Jumbotron.js
In this file, we are going to import jumbotron component from the react-bootstrap package just like below code snippet.
  1. import React, { Component } from 'react';
  2. import { Jumbotron , Button } from 'react-bootstrap';
  3.  
  4. class JumbotronDemo extends Component {
  5. render() {
  6. return (
  7. <div>
  8. <Jumbotron>
  9. <h1>React-bootstrap Jumbotron</h1>
  10. <p>
  11. This is the simple example which shows the simple demo for Jumbotron of react-bootstrap
  12. </p>
  13. <p>
  14. <Button bsStyle="primary">Click Me</Button>
  15. </p>
  16. </Jumbotron>
  17. </div>
  18. );
  19. }
  20. }
  21.  
  22. export default JumbotronDemo;
Carousel.js
In this file same way, we need to import carousel component from a react-bootstrap package like this.
  1. import React, { Component } from 'react';
  2. import Carousel from 'react-bootstrap/lib/Carousel';
  3.  
  4. class CarouselDemo extends Component {
  5. render() {
  6. return (
  7. <div>
  8. <Carousel>
  9. <Carousel.Item>
  10. <img width={900} height={500} alt="900x500" src="https://dummyimage.com/900x500/000/fff" />
  11. <Carousel.Caption>
  12. <h3>Slide 1 Label</h3>
  13. <p>This is the slide 1 description</p>
  14. </Carousel.Caption>
  15. </Carousel.Item>
  16. <Carousel.Item>
  17. <img width={900} height={500} alt="900x500" src="https://dummyimage.com/900x500/000/fff" />
  18. <Carousel.Caption>
  19. <h3>Slide 2 Label</h3>
  20. <p>This is the slide 2 description</p>
  21. </Carousel.Caption>
  22. </Carousel.Item>
  23. <Carousel.Item>
  24. <img width={900} height={500} alt="900x500" src="https://dummyimage.com/900x500/000/fff" />
  25. <Carousel.Caption>
  26. <h3>Slide 3 Label</h3>
  27. <p>This is the slide 3 description</p>
  28. </Carousel.Caption>
  29. </Carousel.Item>
  30. </Carousel>
  31. </div>
  32. );
  33. }
  34. }
  35.  
  36. export default CarouselDemo;
As you can see that I have included code for Carousel with different other markup tags like.
  • Carousel
  • Carousel.item
  • Carousel.caption
  • Which is used to provide separate slide’s data as well as their appropriate captions based on our requirements.Now it’s time to include these two components in our main component which is App.js, to render both components we need to import in App.js file which looks like this.
App.js
  1. import React, { Component } from 'react';
  2. import './App.css';
  3. import Jumbotron from './react-bootstrap/Jumbotron';
  4. import Carousel from './react-bootstrap/Carousel';
  5.  
  6. class App extends Component {
  7. render() {
  8. return (
  9. <div className="App">
  10. {/* Using Jumbotron */}
  11. <Jumbotron />
  12. {/* Using Carousel */}
  13. <Carousel />
  14. </div>
  15.  
  16. );
  17. }
  18. }
  19. export default App;
Here in this code snippet, I have imported all two different reusable components in order to render their HTML within the main component file, now save it and execute the application, at last, our page looks like this.
In the output, you can see that both two of our components are rendered as expected with appropriate data; this is how we have used different react-bootstrap components within our react application. I would recommend you to use other components which are provided by react-bootstrap and create something amazing out of it.
Summary
So far in this tutorial of using Bootstrap with react we have covered all the necessary steps which show how to use different bootstrap components into our react application, their different third-party libraries, how to install and use inside application, bootstrap can provide any website a decent look and feel, I would recommend you to go through all the steps and apply multiple components with your react application. Thanks for reading.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.