Bootstrap not working with React

Viewed 4206

I'm trying to apply Bootstrap attributes to my <span> and <button>. I'm not sure what I'm doing wrong. I've searched this question here in stackoverflow, but all of the suggestions say to change class to className. I've done this but I'm still unable to see my styles change.

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import Counter from './components/counter';

ReactDOM.render(<Counter/>, document.getElementById('root'));
registerServiceWorker();

counter.jsx

import React, { Component } from 'react';

class Counter extends Component {
    state = {
        count: 0
    };
    render() {
        return (
            <div>
                <span className="badge badge-primary m-2">
                    {this.formatCount()}
                </span>
                <button className="btn btn-secondaty btn- 
sm">Increment</button>
            </div>
        );
    }
    formatCount() {
        const { count } = this.state;
        return count === 0 ? 'Zero' : count;
    }
} 

export default Counter;

Any insight into this problem will be much appreciated.

4 Answers

In bootstrap v5 it's changed to "bg-primary" and "bg-secondary" etc instead, so try className="badge bg-primary m-2".

Assuming you have already installed Bootstrap, then add this import statement to your code:

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

Please check the version of bootstrap, there are few changes in the badge class.

According to Bootstrap v5.0 and above, use the following code for the Badge class.

render() { return ( {this.formatCount()} Increment );

I faced similar problem. Somehow I find that bootstrap version causing trouble to me!

So I uninstalled current version then installed an specific version.

npm remove bootstrap

npm install bootstrap@4.6.0

Then import necessary bootstrap script in App.js file.

import "../node_modules/bootstrap/dist/css/bootstrap.min.css";

or

import "bootstrap/dist/css/bootstrap.min.css";
Related