javascript - ReactJS Picture is not displayed
问题描述
const slides = [
{ id: 0, url: "../img/slide1.jpg" },
{ id: 1, url: "../img/slide2.jpg" },
{ id: 2, url: "../img/slide3.jpg" },
];
export const BannerSlide = () => {
const [index, set] = useState(0)
const transitions = useTransition(slides[index], item => item.id, {
from: { opacity: 0 },
enter: { opacity: 1 },
leave: { opacity: 0 },
config: config.molasses,
});
useEffect(() => void setInterval(() => set(state => (state + 1) % 3), 2000), []);
return transitions.map(({ item, props, key }) => (
<animated.div
key={key}
class="bg"
style={{ ...props, backgroundImage: `url(require("${item.url}")` }}
/>
))
};
Picture is not displayed (backgroundImage: url(require("${item.url}")
)
This works for <img>
tags. Can someone explain the difference between the the two?
Example:
<img src={ Background } />
works just fine.
Thank you!
解决方案
Right now backgroundImage: url(require("${item.url}")
require is not run as a function but as a text.
Try running it as url(${require(item.url)})
and then it will fire function require with good parameter.
推荐阅读
- wordpress - 如何在不同的托管 WP 中复制 ACP Pro 设置?
- pgp - 需要在 Kleopatra 中对 PGP 密钥“更改所有者信任”
- php - PHP:API返回二进制字符串,无法从中创建图像
- javascript - 如何使用 Javascript 重写链接之间的文本
- c# - ASP.NET Core 传递可枚举的对象以在控制器上获取操作
- node.js - Dynamodb 数据库查询中的时间戳比较未按预期工作
- rest - MultiPart PUT 请求在 Spring Boot 中不起作用
- winapi - 如何在 x86 程序集中按值传递结构
- kubernetes - minikube 无法在 mac 上启动
- java - 用 JOLT 通过字典替换数据值