Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add bootstrap link in ReactJS

What is Bootstrap?

Bootstrap, originally named Twitter Blueprint, is a free and open-source CSS framework directed at responsive, mobile-first front-end web development. It contains CSS and JavaScript-based design templates for typography, forms, buttons, navigation, and other interface components. It helps you design websites faster and easier.

What is ReactJS?

ReactJS is an open-source JavaScript library that is used for building user interfaces specifically for single-page applications. It’s used for handling the view layer for web and mobile apps. React also allows you to create reusable UI components.

Why Use Bootstrap with ReactJS?

Bootstrap and ReactJS work together to build powerful and visually appealing web applications. With Bootstrap providing the structure and aesthetic appeal and ReactJS managing the view layer, developers can create dynamic, interactive, and responsive web applications. The combination of these two can save a lot of time and make the design process much more manageable.

How to Add Bootstrap in ReactJS

Before we begin, you should have NodeJS and NPM installed in your system. If not, you can download them from the official website.

Now, let's start with installing Bootstrap in your React application. Navigate to your project folder in your terminal and run the following command:

npm install bootstrap

This command will add Bootstrap to your project dependencies.

Importing Bootstrap into Your Project

Once you've installed bootstrap, you have to import it into your project so that you can use it. To do this, you will add the following line of code at the top of your App.js file:

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

This line of code tells your application to include the Bootstrap CSS file in your application.

Using Bootstrap Components

Now that we have installed and imported Bootstrap into our React application, we can start using its components. Let's create a simple Bootstrap button:

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';

function App() {
  return (
    <button className="btn btn-primary">Click me!</button>
  );
}

export default App;

In the above code snippet, we are using the Bootstrap class .btn and .btn-primary to style our button. When you run your application, you should see a blue Bootstrap button.

Bootstrap and React Components

One of the fundamental aspects of React is the use of components. A component in React is a reusable chunk of code that can be used to build parts of your user interface.

Let's create a Bootstrap card component:

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';

function CardComponent() {
  return (
    <div className="card" style=>
      <img src="card-img.jpg" className="card-img-top" alt="card image"/>
      <div className="card-body">
        <h5 className="card-title">Card title</h5>
        <p className="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        <a href="#" className="btn btn-primary">Go somewhere</a>
      </div>
    </div>
  );
}

export default CardComponent;

The above code is a simple React component that uses Bootstrap classes to create a card. You can reuse this component anywhere in your application by simply importing it.

Conclusion

Imagine Bootstrap as a large Lego set and ReactJS as the playground where you can use these Lego pieces to build your structures. Each Bootstrap class is like a pre-designed Lego piece that you can put together to create your unique structure. The beauty of using Bootstrap with ReactJS is that it allows you to focus on building your application without worrying about styling from scratch. It's like having a head start in a race. Start using Bootstrap with ReactJS and give your applications a professional look while saving time and effort. Remember, it's not just about how your application works, but also how it looks and feels to the user. Happy coding!