javascript - 动态 jquery 图像库不适用于 ReactJS
问题描述
我在 ReactJS 渲染上使用 jquery 有两个带有“画廊”类(所需插件的类)的 div。第一个是由手工代码制作的,并且运行良好。第二个是通过连接变量、字符串和标签标签的循环动态生成的。在第二个 div 中 - 缩略图的图像及其各自的 href(放大的图像版本)完美地出现在屏幕上,但它的动画不起作用。有人知道如何解决吗?
下面调用的函数:
componentDidMount() {
axios.get(URL_INTERIORES)
.then(res => {
this.setState({ interiores: res.data })
})
$(function () {
$('.gallery a').simpleLightbox();
});
}
React 的渲染图:
return (
<div>
// NOTE: This Gallery is working succefully.
<div className="gallery">
<a href="../images/int_02.jpg" className="big">
<img src="../images/int_02_thumb.jpg" alt=""/>
</a>
<a href="../images/int_02.jpg" className="big">
<img src="../images/int_02_thumb.jpg" alt=""/>
</a>
<a href="../images/int_02.jpg" className="big">
<img src="../images/int_02_thumb.jpg" alt=""/>
</a>
</div>
/*
NOTE: This dinamic galley is showed but here isn't
the animation working.
*/
{this.state.interiores.map(item =>
<div>
<div className="gallery">
{
item.fotos
.map(foto =>
<a href={`../images/${foto}.jpg`} className="big">
<img src={`../images/${foto}_thumb.jpg`} alt="" />
</a>
)
}
</div>
</div>
)}
解决方案
我找到了解决方案。我也把相同的函数体componentDidMount()
放在里面
componentDidUpdate()
。现在它正在成功运行。
componentDidMount() {
axios.get(URL_INTERIORES)
.then(res => {
this.setState({ interiores: res.data })
})
$(function () {
$('.gallery a').simpleLightbox();
});
}
// Here is the new code insertion:
componentDidUpdate() {
$(function () {
$('.gallery a').simpleLightbox();
});
}
推荐阅读
- html - Shopify:如何在 2 个地方放置多币种表格(在页眉和 MobileMenu 中 - 用于响应式设计)
- python - 如何在带有两个 DataFrame 的 pandas 中使用替换?
- wpf - 其他元素折叠时 WPF UI 元素不会增长
- maven - Maven/Xtext:找不到字符串资源“_UI_DiagnosticRoot_diagnostic”
- sed - 如何删除单独但不是数据一部分的正斜杠?
- mysql - 需要帮助加入多个表以给出每个表的最大数量?
- javascript - 如何将 document.getElementById(id) 元素转换为 jquery 对象?
- ios - 无法与信息视图通信我的控制器
- javascript - 用酶渲染的组件中的数组未定义?
- android - 如何在一个 apk 中包含多个 PWA?