打字稿反应 - 迭代道具对象
当使用打字稿时,如何通过React(或SolidJ)组件中的对象循环html?是否有正确的语法,或者需要工作?
export default function MyComponent(props: any) {
return (
<>
{Object.values(props.someObject).map(value => {
<div>{value.name}</div> {/* Error: Object is of type 'unknown'. */}
})}
</>
)
}
我正在关注本上下文的SolidJs教程: https://youtu.be/wsvmeg.be/wsvmeg7hpye 特别是ID喜欢使用&lt; for&gt;
循环组件内置在solidjs中,我认为这只是地图的速记语法。
<For each={props.someObject}>
{value =>
<div>{value.name}</div> {/* Error: Object is of type 'unknown'. */}
}
</For>
When using TypeScript how do you loop through an object within a React (or SolidJS) components HTML? is there a correct syntax or does it require a work around?
export default function MyComponent(props: any) {
return (
<>
{Object.values(props.someObject).map(value => {
<div>{value.name}</div> {/* Error: Object is of type 'unknown'. */}
})}
</>
)
}
I'm following this SolidJS tutorial for context: https://youtu.be/WSvmEG7HPyE
Specifically id like to use the <For>
loop component built into SolidJS which I believe is just a shorthand syntax of map.
<For each={props.someObject}>
{value =>
<div>{value.name}</div> {/* Error: Object is of type 'unknown'. */}
}
</For>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样的简单函数对于获取具有正确类型的道具条目很有用:
然后,它可以使用
&lt; for&gt;
组件来映射道具:in游乐场
A simple function like this would be useful for getting the props entries with correct types:
Then it could be used to map over props using the
<For>
component:demo in playground
在React + TypeScript中,您需要以以下方式进行。
In React + typescript you would need to do it in the following way.