How to include bootstrap css and js in reactjs app?

后端 未结 19 1027
既然无缘
既然无缘 2020-12-04 05:07

I am newb to reactjs, I want to include bootstrap in my react app

I have installed bootstrap by npm install bootstrap --save

Now, want to load b

相关标签:
19条回答
  • 2020-12-04 05:46

    Somehow the accepted answer is only talking about including css file from bootstrap.

    But I think this question is related to the one here - Bootstrap Dropdown not working in React

    There are couple of answers that can help -

    1. https://stackoverflow.com/a/52251319/4266842
    2. https://stackoverflow.com/a/54188034/4266842
    0 讨论(0)
  • 2020-12-04 05:47

    Full answer to give you jquery and bootstrap since jquery is a requirement for bootstrap.js:

    Install jquery and bootstrap into node_modules:

    npm install bootstrap
    npm install jquery
    

    Import in your components using this exact filepath:

    import 'jquery/src/jquery'; //for bootstrap.min.js
    //bootstrap-theme file for bootstrap 3 only
    import 'bootstrap/dist/css/bootstrap-theme.min.css';
    import 'bootstrap/dist/css/bootstrap.min.css';
    import 'bootstrap/dist/js/bootstrap.min.js';
    
    0 讨论(0)
  • 2020-12-04 05:47

    After installing bootstrap in your project "npm install --save bootstrap@3.3.7" you have to move to the index.js file in the project SRC folder and import bootstrap from node module package.

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

    If you like you can get help from this video, sure it will help you a lot.

    Import Bootstrap In React Project

    0 讨论(0)
  • 2020-12-04 05:47

    Just in case if you can use yarn which is recommended for React apps,

    you can do,

    yarn add bootstrap@4.1.1 // this is to add bootstrap with a specific  version
    

    This will add the bootstrap as a dependency to your package.json as well.

    {
      "name": "my-app",
      "version": "0.1.0",
      "private": true,
      "dependencies": {
        "bootstrap": "4.1.1", // it will add an entry here
        "react": "^16.5.2",
        "react-dom": "^16.5.2",
        "react-scripts": "1.1.5"
      },
      "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject"
      }
    }
    

    After that just import bootstrap in your index.js file.

    import 'bootstrap/dist/css/bootstrap.css';
    
    0 讨论(0)
  • 2020-12-04 05:48

    I try with instruction:

    npm install bootstrap
    npm install jquery popper.js
    

    then in src/index.js:

    import 'bootstrap/dist/css/bootstrap.min.css';
    import $ from 'jquery';
    import Popper from 'popper.js';
    import 'bootstrap/dist/js/bootstrap.bundle.min';
    import React from 'react';
    import ReactDOM from 'react-dom';
    import './index.css';
    import App from './App';
    import registerServiceWorker from './registerServiceWorker';
    
    ReactDOM.render(<Dropdown />, document.getElementById('root'));
    registerServiceWorker();
    
    0 讨论(0)
  • 2020-12-04 05:48

    Simple Solution (2020) is as follows :-

    1. npm install --save jquery popper.js
    2. npm install --save bootstrap
    3. npm install --save style-loader css-loader
    4. update webpack.config.js, you've to tell webpack how to handle the bootstrap CSS files hence you've to add some rules as shown :-

    1. Add some imports in the entry point of your React Application (usually it's index.js), place them at the top make sure you don't change the order.

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

    import $ from "jquery";

    import Popper from "popper.js";

    import "bootstrap/dist/js/bootstrap.bundle.min";

    1. These steps should help you out, please comment if there's some problem.
    0 讨论(0)
提交回复
热议问题