在 React 中渲染对象中的所有嵌套对象
2022-06-21
61
我这里有这个对象:
const [generic, setGeneric] = useState ({
f1: "Hi, Stack Overflow!",
f2: "Hello!",
});
我想从中访问所有子对象。
我这里有一些代码,但它不能正常工作:
{Object.keys(generic).map((gen) => (
<>
<h1 key={gen}>
{/* I also tried rendering files instead of gen, just don't work */}
</>
))
预期结果是:
<h1>Hi, Stack Overflow!</h1>
<h1>Hello</h1>
2个回答
{Object.entries(generic).map(([key, value]) => (
<h1 key={key}>{value}</h1>
))}
Andyally
2022-06-21
尝试一下
{Object.keys(generic).map((gen,index) => (
<h1 key={index}>{generic[gen]}</h1>
))}
Alì Shadman
2022-06-21