reactjs - 解决了 React,将对象作为参数传递给页面,不在 html 中显示,但会在控制台中
问题描述
我只是将对象从一页传递到另一页。然后我获取对象并循环遍历它以在 html 中显示它。它不会在 html 中显示任何内容,但会 console.log 它。这是代码。query.tags 是一个数组。我尝试使用状态来进行测试,但它并没有改变任何东西。
const {query} = useRouter();
console.log(query.id, query.title, query.description, query.segment);
const [stateTags, setStateTags] = useState(query.tags);
const tags = () => {
console.log(stateTags);
console.log("new here--", query.tags);
query.tags?.map((tag, index) => {
console.log(tag)
return (
<Tag key={index}>{tag}</Tag>
);
});
};
return (
<div>{query.tags?.map((tag, index) => {
return (
<Tag key={index}>{tag}</Tag>
);
})}</div>
);
有人可以向我解释为什么它会显示在控制台中,但不会显示在 dom/html 中。解决了
解决方案
您的标签函数没有返回任何内容。尝试在 map 函数之前返回 add。
尝试:-
return query.tags?.map((tag, index) => (
<Tag key={index}>{tag}</Tag>
));
推荐阅读
- database - Mongodb查找和观看
- bluetooth - 音频流中断
- javascript - 第一次单击时反应开关输入未触发 SetState
- python - 从未传递给 __init__ 方法的类继承属性
- javascript - 为什么我的警报在 if() 中时没有弹出?
- python - The Osmnx python graph_to_gdfs sometimes returns a list in the name column, what's causing this?
- c# - 从 .NET Core 2.0 迁移到 3.1 后,jQuery.Ajax 调用返回“404 Not Found”
- node.js - 如何将节点中的请求发送到使用 grpc-web-proto 的端点?
- javascript - 在 AXIOS 调用 PHP 之后,对象数组变成字符串数组
- microservices - 3 层架构(单体?)与微服务