{"id":5597,"date":"2020-10-01T11:58:00","date_gmt":"2020-10-01T11:58:00","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=5597"},"modified":"2021-06-19T11:24:22","modified_gmt":"2021-06-19T11:24:22","slug":"how-to-use-bootstrap-with-react","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/tutorial\/how-to-use-bootstrap-with-react\/","title":{"rendered":"How to use Bootstrap with React?"},"content":{"rendered":"\n<p>When building a powerful web application, you are undoubtedly aware that <strong>Bootstrap <\/strong>and <strong>ReactJS<\/strong> continue to be the most popular choice. Using native <strong>Bootstrap<\/strong> with <strong>ReactJS<\/strong> is not as easy as it seems; however, different methods can be used.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<h2 id=\"why-is-it-challenging-to-include-bootstrap-components-with-react\"><strong>Why is it challenging to include Bootstrap components with React?<\/strong><gwmw style=\"display:none;\"><\/gwmw><\/h2>\n\n\n\n<p>React is a simple library that creates interactive front end web pages, with a component-based architecture that can be nested or put side by side. Hence, adding third party libraries to React is easy.<gwmw style=\"display:none;\"><gwmw style=\"display:none;\"><\/gwmw><\/gwmw><\/p>\n\n\n\n<p>However, Bootstrap relies heavily on jQuery for powering its user interface components. Since jQuery manipulates the Document Object Model (DOM) directly, Bootstrap contradicts the declarative approach inherent to React. If you are looking to include a vanilla Bootstrap stylesheet with a responsive 12 column grid or components that do not involve jQuery, you can add those components to React with ease. However, components that rely on jQuery cannot be integrated in such a manner.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>Hence, to use Bootstrap with ReactJS, there must be a way to remove Bootstrap\u2019s jQuery dependency.<\/p>\n\n\n\n<h2 id=\"how-to-use-bootstrap-with-reactjs\"><strong>How to use Bootstrap with ReactJS?<\/strong><gwmw style=\"display:none;\"><\/gwmw><\/h2>\n\n\n\n<h3 id=\"option-1-including-raw-css-bootstrap-files-into-reactjs\"><strong>Option 01:&nbsp;Including raw CSS Bootstrap files into ReactJS<\/strong><gwmw style=\"display:none;\"><\/gwmw><\/h3>\n\n\n\n<p>The first option to use Bootstrap with ReactJS is to include raw CSS Bootstrap files with ReactJS.<gwmw style=\"display:none;\"><gwmw style=\"display:none;\"><\/gwmw><\/gwmw><\/p>\n\n\n\n<p>1. Create a new default Create React App project in Your system using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npx create-react-app my-project<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p>The above command creates the project using the base template. To create the project using other available templates, execute the following command:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npx create-react-app my-project \u2013template [template-name]<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p>You can find a list of available templates by searching for \u201ccra-template-*\u201d on npm. Templates are usually named in the format \u201ccra-template-[template-name]. Simply enter the desired template name into the command above.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>2. Run the new project using the following commands<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>Navigate to the new project using the following command:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>cd my-project<\/em><gwmw style=\"display:none;\"><gwmw style=\"display:none;\"><\/gwmw><\/gwmw><\/pre>\n\n\n\n<p>Start npm using the following command:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npm start<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p>3. Link the Bootstrap stylesheet to ReactJS.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>Install Bootstrap using the following command:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npm install bootstrap<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p>Include the following code in your&nbsp;<em>src\/index.js<\/em>&nbsp;or&nbsp;<em>App.js&nbsp;<\/em>file of the &nbsp;<em>create-react-app<\/em>&nbsp;package:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>import \u201cbootstrap\/dist\/bootstrap.min.css\u201d;<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p>You can now use Bootstrap classes with ReactJS code after the Bootstrap stylesheet is successfully integrated into a React app.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>To best use ReactJS, it would be ideal to import Bootstrap classes as React components. Luckily, this is done by utilizing third-party libraries like&nbsp;<em>react-bootstrap<\/em>&nbsp;and&nbsp;<em>reactstrap,<\/em>&nbsp;to do so.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<h3 id=\"option-2-using-the-react-bootstrap-library\"><strong>Option 02:&nbsp;Using the react-bootstrap library<\/strong><gwmw style=\"display:none;\"><\/gwmw><\/h3>\n\n\n\n<p>The&nbsp;<em>react-bootstrap<\/em>&nbsp;library is one of the most used libraries that replaced Bootstrap JavaScript and has been built from scratch as a true React component, by removing jQuery dependencies. The latest version of the library only supports Bootstrap v4.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p><strong>Installing the react-bootstrap library<\/strong><gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>To use the library, you can install it using the following command:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npm install react-bootstrap bootstrap<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p><strong>Using the components of react-bootstrap<\/strong><gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>To use the components, simply import individual components like&nbsp;<em>react-bootstrap\/Button<\/em>&nbsp;instead of the entire library. This ensures that you only pull the components you use, thus reducing the code you send to the client.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>import Button from \u201creact-bootstrap\/Button\u201d;<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p>The available components are alerts, accordion, badge, breadcrumb, buttons, button group, cards, carousel, dropdowns, forms, input group, images, figures, jumbotron, list group, modal, navs, navbar, overlays, pagination, popovers, progress, spinners, table, tables, tooltips, and toasts.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p><strong>Importing Stylesheets<\/strong><gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>While&nbsp;<em>react-bootstrap<\/em>&nbsp;does not necessarily depend on a very precise version of Bootstrap, some stylesheet is still required to use these components. Run the following command:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npm install bootstrap<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p>Now, you can include the following code in your&nbsp;<em>src\/index.js<\/em>&nbsp;or&nbsp;<em>App.js&nbsp;<\/em>file:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>import \u201cbootstrap\/dist\/css\/bootstrap.min.css\u201d;<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p><strong>Sass<\/strong><gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>If you are using Sass, make sure you install the node-sass package. You can then include Bootstrap\u2019s source Sass files in your main Sass file. This is applicable to the typical&nbsp;<em>create-react-app&nbsp;<\/em>application. Include the following code in your&nbsp;<em>src\/App.scss<\/em>&nbsp;file.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>import \u201c~bootstrap\/scss\/bootstrap\u201d;<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<h3 id=\"option-3-using-the-reactstrap-library\"><strong>Option 03:&nbsp;Using the reactstrap library<\/strong><\/h3>\n\n\n\n<p>The&nbsp;<em>reactstrap<\/em>&nbsp;library has specifically been created for use with Bootstrap v4, and it allows you to import relevant Bootstrap components into your ReactJS project.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p><strong>Installing the reactstrap library<\/strong><gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>To use the library, you can install it using the following command:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npm install reactstrap react react-dom<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p><strong>Using the components of reactstrap<\/strong><\/p>\n\n\n\n<p>To use the relevant components, you can simply import the required components instead of the entire library. This ensures that you only pull the components you use, thus reducing the code you send to the client. For example:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>import { Button } from \u2018reactstrap\u2019;<\/em><gwmw style=\"display:none;\"><gwmw style=\"display:none;\"><\/gwmw><\/gwmw><\/pre>\n\n\n\n<p>The available components are alerts, badge, breadcrumbs, button dropdown, button group, button, card, carousel, collapse, dropdowns, fade, form, input group, jumbotron, layout, list group, media, modals, navbar, navs, pagination, popovers, progress, spinners, tables, tabs, toasts, and tooltips.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p><strong>Importing Stylesheets<\/strong><\/p>\n\n\n\n<p>To make the library work as expected, you need to add the Bootstrap CSS manually. Run the following command:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npm install bootstrap<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p>And, import Bootstrap CSS into your reactstrap\u2019s&nbsp;<em>src\/index.js<\/em>&nbsp;file by including the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>import \u201cbootstrap\/dist\/css\/bootstrap.css\u201d;<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<h3 id=\"option-4-other-third-party-modules\"><strong>Option 04:&nbsp;Other Third-party Modules<\/strong><gwmw style=\"display:none;\"><\/gwmw><\/h3>\n\n\n\n<p>There are additional options to&nbsp;<em>react-bootstrap<\/em>&nbsp;and&nbsp;<em>reactstrap<\/em>&nbsp;that can be used to effectively use new Bootstrap like components with ReactJS. Examples include the likes of&nbsp;<em>react-bootstrap-table2<\/em>, a rebuild of&nbsp;<em>react-bootstrap-table<\/em>.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<p>As the name suggests,&nbsp;<em>react-bootstrap-table2<\/em>&nbsp;is used explicitly for creating dynamic tables for web applications. As the name is already taken on npm, the installation of this module requires running the following command:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npm install react-bootstrap-table-next<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<p>After installation, you need to add the&nbsp;<em>react-bootstrap-table2<\/em>&nbsp;CSS into your application before using the module. Add the following code before you use the module:<gwmw style=\"display:none;\"><\/gwmw><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>import \u201creact-bootstrap-table-next\/dist\/react-bootstrap-table2.min.css\u201d;<\/em><gwmw style=\"display:none;\"><\/gwmw><\/pre>\n\n\n\n<h2 id=\"conclusion\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>The option you use to include <strong>Bootstrap <\/strong>with <strong>ReactJS <\/strong>depends on the version of <strong>Bootstrap<\/strong> you are using, the kind of components required, and the flexibility you desire.<gwmw style=\"display:none;\"><\/gwmw><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When building a powerful web application, you are undoubtedly aware that Bootstrap and ReactJS continue to be the most popular choice. Using native Bootstrap with ReactJS is not as easy as it seems; however, different methods can be used. Why is it challenging to include Bootstrap components with React? React is a simple library that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":5598,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[145,62],"tags":[107,146,133,110,217],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/09\/how-to-use-bootstrap-with-react.jpg","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-1sh","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5597"}],"collection":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/comments?post=5597"}],"version-history":[{"count":2,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5597\/revisions"}],"predecessor-version":[{"id":6255,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5597\/revisions\/6255"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/5598"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=5597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=5597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=5597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}