我正在转换一个静态的HTML
站点,它使用bootstrap到React.js。这里有几个只在数据目标和数据切换上打开的div。
<div className="card-header" id="headingFive">
<h5 className="mb-0">
<button
className="btn btn-link"
type="button"
data-toggle="collapse"
data-target="#collapseFive"
aria-expanded="true"
aria-controls="collapseOne">
Site Wide Events
</button>
</h5>
</div>
<div
id="collapseFive"
className="collapse show"
aria-labelledby="headingFive"
data-parent="#accordionThree">
<div className="card-body">
<div className="row">
<div className="col wall">
<h4 className="text-center">12</h4>
<p className="text-center">Target</p>
</div>
<div className="col">
<h4 className="text-center">13</h4>
<p className="text-center">Actual</p>
</div>
</div>
</div>
</div>
我不想使用任何其他npm
模块来实现相同的功能。
我试过这个,但无法解决。
componentDidUpdate() {
$('.collapse').bootstrapToggle();
}
4条答案
按热度按时间baubqpgj1#
Bootstrap 5(2020年更新)
不再需要jQuery,因此Bootstrap 5更易于在React中使用。使用新命名空间
data-bs-
属性explained here * 或 *,并使用React的useEffect useState钩子,如this answer中所述。引导法4(原始问题)
如果不想使用jQuery或react-bootstrap,可以创建一个方法来切换折叠导航栏上的
show
类,如下所示...Example with Navbar Collapse
Example with Collapse
ttp71kqs2#
//在
App.js
中导入以下内容引导依赖项https://getbootstrap.com/docs/4.0/getting-started/introduction/#js
注意:我假设你的结构是正确的。
cedebl8k3#
把
data-toggle
改成data-bs-toggle
,把data-target
改成data-bs-target
。我自己找了很久,到处都找不到解决办法,才设法找到这个!jc3wubiy4#
使用新的命名空间数据-bs- attributes,例如。