我是ReactJS的新手,我想在我的React应用程序中包括引导

我已经通过npm安装bootstrap -save安装了bootstrap

现在,我想在React应用程序中加载引导CSS和JS。

我正在使用webpack。

webpack.config.js

var config = {
    entry: './src/index',

    output: {
        path: './',
        filename: 'index.js'
    },

    devServer: {
        inline: true,
        port: 8080,
        historyApiFallback: true,
        contentBase:'./src'
    },

    module: {
        loaders: [
            {
                test: /\.js?$/,
                exclude: /node_modules/,
                loader: 'babel',

                query: {
                    presets: ['es2015', 'react']
               }
            }
        ]
    }
};

module. exports = config;

我的问题是“如何从节点模块中包含引导CSS和JS在ReactJS应用程序中?”如何设置引导包括在我的React应用程序?


当前回答

下面是我用npm的bootstrap 4所做的工作

安装jquery, popper.js, bootstrap包 NPM安装jquery popper.js bootstrap—save 使用以下导入更新index.js 导入“引导/ dist / css / bootstrap.min.css”; 导入“引导/ dist / js / bootstrap.js”;

此外,如果您使用sass自定义样式,您可以安装sass包

npm install sass --save

其他回答

以防万一,如果你可以使用React应用程序推荐使用的yarn,

你可以,

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

这将把引导程序作为依赖项添加到包中。Json也是。

{
  "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"
  }
}

然后在index.js文件中导入bootstrap。

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

我尝试指导:

npm install bootstrap
npm install jquery popper.js

然后在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();

你不能在React应用程序中使用基于Jquery的Bootstrap Javascript组件,因为任何试图在React之外操作DOM的行为都被认为是糟糕的做法。在这里你可以阅读更多关于它的信息。

如何在React应用程序中包含Bootstrap:

1)推荐。您可以包括Bootstrap的原始CSS文件作为其他答案指定。

2)看看react-bootstrap库。它是专门为React编写的。

3) Bootstrap 4有反作用带

奖金

这些天我通常避免使用Bootstrap库,因为它提供了随时可用的组件(上面提到的react-bootstrap或reactstrap等等)。当您变得依赖于它们所采用的道具时(您不能在其中添加自定义行为),您就失去了对这些组件生成的DOM的控制。

所以要么只使用Bootstrap CSS,要么不使用Bootstrap。一般的经验法则是:如果你不确定你是否需要它,你就不需要它。我见过很多应用程序,其中包括Bootstrap,但只使用少量的CSS,有时大部分CSS被自定义样式覆盖。

某种程度上,公认的答案是只谈论包括css文件从bootstrap。

但是我认为这个问题和这个有关 引导下拉在React中不工作

有几个答案可以帮助你

https://stackoverflow.com/a/52251319/4266842 https://stackoverflow.com/a/54188034/4266842

下面是我用npm的bootstrap 4所做的工作

安装jquery, popper.js, bootstrap包 NPM安装jquery popper.js bootstrap—save 使用以下导入更新index.js 导入“引导/ dist / css / bootstrap.min.css”; 导入“引导/ dist / js / bootstrap.js”;

此外,如果您使用sass自定义样式,您可以安装sass包

npm install sass --save