reactjs - React.Children.toArray 无法在使用 typescript 的 child 身上找到 Prop

标签 reactjs typescript

Typescript 无法使用 React.Children.toArray 检测 child 身上的 Prop 对象 我必须使用 any[] 因为如果我使用 ReactChild[] - 在数组项上未检测到 Prop 。我该如何正确输入? 谢谢!

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']

enter image description here

最佳答案

casieber 的回答是正确的。

但为了使用 typescript 访问您的 Prop ,这是必需的:

const item = items[0];
if (React.isValidElement<{prop1: boolean}>(item)) {

    // item.props.prop1 -- works
}

关于reactjs - React.Children.toArray 无法在使用 typescript 的 child 身上找到 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52524093/

相关文章:

angular - 在 Angular 2 的 ng2-smart-table 的操作列中添加自定义按钮

typescript - 为什么我的服务未定义?

reactjs - 将 PubSubJS 与 Jest/Enzyme 一起使用时,如何模拟 React 组件上的发布/订阅事件?

javascript - TS |元素隐式具有 'any' 类型,因为类型 'string' 的表达式不能用于索引类型 'Record<SecurityMode, boolean>'

Typescript:Promise 的子类/扩展:不引用 Promise 兼容的构造函数值

javascript - 返回 React 组件中索引中的第一项

reactjs - 在功能组件中声明没有 useState 的变量可以吗? react Hook

reactjs - 将 p5 包导入到 React 功能组件中?

reactjs - 无法加载脚本。请确保您正在运行 Metro 服务器或您的 bundle 'index.android.bundle' 已正确打包以供发布

typescript - TypeScript 中的字典类型