根据数据库返回的值有条件地渲染图像源
试图根据数据库返回的值的价值,将我的头缠绕着更改图像源的过程。
摘要:
function KnifesComponent() {
const knifeCollection = collection(db, "knives");
const [knives, setKnives] = useState([]);
useEffect(() => {
onSnapshot(knifeCollection, (snapshot) => {
setKnives(snapshot.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
});
}, []);
return (
{knives.map((skin) => {
return (
{skin.rarity} // Returns "Ultra" for Example
<div><img src={isUltra : require('../../public/Rarity_Ultra.png')}></div>
);
}
我正在考虑制作具有所有值的const,然后根据需要使用所有值,但不确定如何确切地接近它
const rarities = {
Exclusive: require('../../public/Rarity_Exclusive.png'),
Ultra: require('../../public/Rarity_Ultra.png'),
}
是什么是根据值将不同图像进行条件格式化不同图像的正确方法?
Trying to wrap my head around what the process would be to change an image source depending on the value of what is returned from the database.
Snippet:
function KnifesComponent() {
const knifeCollection = collection(db, "knives");
const [knives, setKnives] = useState([]);
useEffect(() => {
onSnapshot(knifeCollection, (snapshot) => {
setKnives(snapshot.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
});
}, []);
return (
{knives.map((skin) => {
return (
{skin.rarity} // Returns "Ultra" for Example
<div><img src={isUltra : require('../../public/Rarity_Ultra.png')}></div>
);
}
I was thinking of making a const with all the values in and then using as needed but not sure how exactly to approach it
const rarities = {
Exclusive: require('../../public/Rarity_Exclusive.png'),
Ultra: require('../../public/Rarity_Ultra.png'),
}
What is the proper way to conditionally format different images based on values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将稀有物对象更改为此(路径为值):
然后,您可以通过稀有性访问对象的图像。
对象也可以是这样(只有图像名称):
并访问它们:
You can change the rarities object to this (paths as values):
Then you can then access the image from the object by rarity.
The object also can be like this (just the image names):
And access them: