16

Use Bootstrap in ReactJS – A complete beginners guide

 3 years ago
source link: https://www.simplifiedcoding.net/use-bootstrap-in-reactjs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
use bootstrap in react js

Use Bootstrap in ReactJS – A complete beginners guide

This post is about how to use bootstrap in react js and we will see it with an example.

Bootstrap is one of the most popular CSS frameworks and it is the seventh most starred project on Github, with more than 142,000 stars.

In this post, we are going to learn how to use bootstrap in react js project and make our react app responsive and beautiful.

We can use bootstrap in multiple ways, some of them are:

  1. Using Bootstrap CDN
  2. Installing Bootstrap as a dependency
  3. Installing Reactstrap as a dependency

Use bootstrap in React JS as CDN

use bootstrap in react js

This is one of the easiest ways to use bootstrap in react js website. You just have to visit getbootstrap.com and there you’ll find the link to the CDN of Bootstrap.

<!-- Bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
    integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

Copy the CDN link and paste it in the index.html of your react app.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="theme-color" content="#000000" />
  <meta name="description" content="Web site created using create-react-app" />
  <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
  <!-- Bootstrap -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
    integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
  <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
  <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.
      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
  <title>React App</title>
</head>
<body>
  <noscript>You need to enable JavaScript to run this app.</noscript>
  <div id="root"></div>
  <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.
      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.
      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
</body>
</html>

This is one of the easiest ways of using Bootstrap with React JS.

Installing Bootstrap as a dependency

The second way of using Bootstrap is to install into your project with the help of npm.

npm install bootstrap

This is going to install Bootstrap into your react project and you can see the bootstrap folder inside node_modules folder.

Now to import bootstrap into your file.

// import react
import React from "react";
// import Bootstrap
import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
export default function App() {
  return (
    <div className="container mt-5">
      <h1>Hello World</h1>
      <button className="btn btn-primary">Welcome</button>
    </div>

With the help of the above code, you’ll get output something similar to this.

npm-install-bootstrap-output-1024x576.png

This is also one of the preferred ways of using bootstrap via npm.

Installing Reactstrap as a dependency

This is the most preferred way of using bootstrap with react. It provides easy to use React Bootstrap 4 components. You can install reactstrap and can easily import bootstrap components and use them in your app.

To install reactstrap in your react app, use the below npm command.

npm install – save reactstrap

Note: Reactstrap does not include Bootstrap CSS so this needs to be installed as well.

npm install --save bootstrap
npm install --save reactstrap

After installing reactstrap into your project, you need to import bootstrap into your src/index.js file.

import 'bootstrap/dist/css/bootstrap.min.css';

import React from "react";
import ReactDOM from "react-dom";
// import bootstrap
import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
import App from "./App";
ReactDOM.render(<App />, document.getElementById("root"));

Next step is to import required reactstrap components within src/App.js file or your custom component file.

import { Button } from reactstrap

Or the complete code of src/app.jsx

import React from "react";
import { Button } from "reactstrap";
export default (props) => {
  return <Button color="primary">Danger!</Button>

Well, this was all about how you can use Bootstrap in React JS and I hope it has helped you.

If you find this post helpful, please share it with your friends and also subscribe to our channel.

If you stuck into any kind of error, don’t forget to google it and try to debug it on your own. This will teach you how to debug on your own as someone might have faced a similar problem earlier.

If you still don’t find any solution for your problem, you can ask your doubt in the comment’s section below and we’ll get back to you🤓.

Thanks for your visit and if you are new here, consider subscribing to our newsletter. See you in my next post. Take care, Bye!

I’m Pratik Sah, a proud Indian full stack developer who loves coding and a die-hard fan of Chelsea ⚽ FC. It was nice meeting you. – ThePratikSah ✌️

Checkout these tutorials as well:

  • Android Game Development Course for Beginners
  • Complete React Js environment setup in under 15 minutes
Categories ReactJS Post navigation

Leave a Comment Cancel reply

Comment

Name Email

Save my name, email, and website in this browser for the next time I comment.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK