4

I want to use Bootstrap for creating a table in ReactJS. I have written the code bellow and have used class="container" in <div> and class="table table-striped" in <table> but the bootstrap is not working in this code. Please tell what can be the issue?

app.js

render : function() {
        return (
            <div class="container">
                <table class="table table-striped">
                    <tbody>
                    {this.state.rows.map((r) => (
                        <tr>
                            <td>{r}</td>
                            <td>
                                <button onClick={() => this.deleteRow(r)}>Delete</button>
                            </td>
                        </tr>
                    ))}
                    </tbody>
                </table>
                <input trype="text" id={"newVal"} onChange={this.updateNewValue}></input>
                <button id="addBtn" onClick={this.addRow}>ADD</button>
            </div>
        );
    },

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sample Project</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <link href="http://maxcdn.bootstrapcdn.com/bootswatch/3.3.4/flatly/bootstrap.min.css" type="text/css" rel="stylesheet" />

    <script src="app.js" type="text/babel"></script>
</head>
<body>
    <div id="display"></div>
</body>
</html>
2
  • 1
    Please check the console in your browser when you see unexpected behaviour. There should be a message there telling you exactly what's wrong here. Commented Sep 19, 2017 at 5:56
  • 2
    Possible duplicate of Why is react removing my class names? Commented Sep 19, 2017 at 5:57

5 Answers 5

8

Import Bootstrap CSS and optionally Bootstrap theme CSS at the beginning of your src/index.js file:

import 'bootstrap/dist/css/bootstrap.css';
Sign up to request clarification or add additional context in comments.

Comments

2

In jsx you should use the className key word instead of class.
For example:
<table className="table table-striped">

You can read about it in the DOCS.

Comments

1

To specify a CSS class, use the className attribute. This applies to all regular DOM and SVG elements like <div>, <a>, and others.

If you use React with Web Components (which is uncommon), use the class attribute instead.

Comments

1

By default npm install will install latest version of bootstrap that is version 5. here we need to enable version 4 in order to work and here is the list of steps to follow.

  1. to uninstall bootstrap 5 npm uninstall bootstrap

  2. Install any bootstrap version 4 series. npm install [email protected]

Now import Bootstrap CSS in your src/index.js file:

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

Comments

0

I faced the similar issue in 2022.Simply uninstalling bootstrap library and installing lower version like 4.x.x works for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.