css - materialize.css 中的 materialboxed 不起作用
问题描述
我正在尝试使用 materialboxed 和 materialize.css 在我的反应应用程序中将我的图像作为灯箱打开,但它不起作用。人们告诉我querySelectorAll
在我的中使用,useEffect
但这也不起作用。这是我的代码
import React, {Fragment, useContext, useEffect} from 'react';
import {GlobalContext} from "../../context/GlobalState";
import Parallax from "../layout/Paralex";
import {Link} from "react-router-dom";
import M from 'materialize-css/dist/js/materialize.min.js';
const Pillow = ({location, match}) => {
const {getPillow, pillow} = useContext(GlobalContext);
useEffect(async () => {
getPillow(match.params.id);
const elems = await document.querySelectorAll('.materialboxed');
M.Materialbox.init(elems)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
if (!pillow) {
return null
}
return (
<Fragment>
<Parallax pillow={pillow}/>
<br/>
<div className="row">
<div className="col s12 m6">
<div className="btn white green-text waves-effect waves-red">
<Link to="/pillows">Back to pillows</Link>
</div>
</div>
</div>
<div className="container">
<div className="row">
{pillow.galleryImages.map(img => (
<div key={img.imageUrl} className="col s12 m1">
<img className="materialboxed responsive-img" src={img.imageUrl} alt=""/>
</div>
))}
</div>
请协助。谢谢 !!
解决方案
为您的图像标签创建一个引用,然后在其中初始化材料箱。
const imageRef = React.useRef();
React.useEffect(() => {
M.Materialbox.init(imageRef.current);
}, [imageRef]);
您的图片标签
<img className="materialboxed responsive-img" src={img.imageUrl} alt="" ref={imageRef}/>
它现在应该可以工作了。
推荐阅读
- excel - Microsoft Excel - 如果您制定了适用于特定选择的新条件格式规则,如何在每张工作表上应用相同的规则?
- sharepoint - 通过 Power Automate 发送电子邮件
- kubernetes - 由于 efs 驱动程序问题,Jenkins 应用程序未启动
- google-apps-script - 取消保护错误消息时:目标范围和源范围必须在同一个电子表格上
- ios - SwiftUI - 显示一次后从一开始就为文本设置动画
- maven - 为 Gradle IntelliJ 插件项目添加存储库插件
- ubuntu - 如何在 ubuntu 20.04 焦点上下载 dotnet-sdk-2.2?
- express - 为什么某些 HTTP 标头需要在 express 中明确允许?
- python - 包含括号的表达式出现错误
- firebase - Firebase 托管重写为 html 路由文件