twitter-bootstrap 如何插入 bootstrap.js 以响应应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43722322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert bootstrap.js to react app?
提问by Stanislav Mayorov
How to insert twitter-bootstrap js file to react app using webpack?
如何插入 twitter-bootstrap js 文件以使用 webpack 响应应用程序?
I know about react libraries like reactstrap and react-bootstrap, but I want to know how to insert bootstrap.js file without these libs.
我知道reactstrap 和 react-bootstrap等反应库,但我想知道如何在没有这些库的情况下插入 bootstrap.js 文件。
回答by Ethan Brown
You can just install Bootstrap and jQuery via npm/yarn:
你可以通过 npm/yarn 安装 Bootstrap 和 jQuery:
npm install bootstrap jquery
Then import Bootstrap in your top-level React file (the entry point):
然后在顶级 React 文件(入口点)中导入 Bootstrap:
import 'bootstrap/dist/css/bootstrap.min.css'
import 'jquery/dist/jquery.min.js'
import 'bootstrap/dist/js/bootstrap.min.js'
You'll need the appropriate Webpack rules to transform CSS, of course.
当然,您需要适当的 Webpack 规则来转换 CSS。
The problem with using Bootstrap in React is that it requires jQuery, and now you have two things mucking with the DOM, which is a recipe for trouble. That's the real value proposition of Reactstrap (for Bootstrap 4) and React-Bootstrap (which also supports Bootstrap 4 now); they remove the dependence on jQuery, and implement the functionality as React components. I highlyrecommend you leverage the work of these projects; you're opening a can of worms by trying to combine jQuery with React. It's possible; I've done it; I don't recommend it.
在 React 中使用 Bootstrap 的问题在于它需要 jQuery,而现在你有两件事与 DOM 相关,这是一个麻烦的秘诀。这就是 Reactstrap(用于 Bootstrap 4)和 React-Bootstrap(现在也支持 Bootstrap 4)的真正价值主张;他们消除了对 jQuery 的依赖,并将功能实现为 React 组件。我强烈建议您利用这些项目的工作;通过尝试将 jQuery 与 React 结合,你正在打开一堆蠕虫。这是可能的; 我已经做到了;我不推荐它。

