reactjs - React.Children.toArray 找不到使用打字稿的孩子的道具
问题描述
Typescript 无法使用 React.Children.toArray 检测子级的道具对象,我必须使用 any[] 因为如果我使用 ReactChild[] - 在数组项上未检测到道具。如何正确输入?谢谢!
const items: any[] = React.Children.toArray(this.props.children)
// select whatever item was told to be onload first
const itemPaymentType = items[0].props['data-payment']
解决方案
卡西伯的答案是正确的。
但为了使用打字稿访问您的道具,需要这样做:
const item = items[0];
if (React.isValidElement<{prop1: boolean}>(item)) {
// item.props.prop1 -- works
}
推荐阅读
- assembly - 在寻址模式下无效使用寄存器
- jquery - 重新初始化 Magnific Popup 喷气背包无限滚动
- c++ - VS 2017 C++ 编译器在 VS 2005 中找不到匹配的函数
- asp.net-core - 在 centos 退出时启用 Kestrel 服务 (code=exited, status=145)
- java - 无法在 java 中读取文本文件(视为空文件)
- ios - AVPlayer : 淡入视频的音频
- jquery - 如何遍历 Jquery 中的 JSON 对象以匹配字符串然后返回特定值?
- faunadb - 如何选择区域/动态站点位置 FaunaDB
- reactjs - 在 Redux 中 Action 不会触发 reducer
- google-cloud-platform - Titan 安全密钥 - 我在哪里可以买到?