【发布时间】:2021-09-01 04:53:37
【问题描述】:
我正在努力访问 React 中的嵌套 prop 对象
我有一个 Header 可重用组件,其中的道具是 className 和 title。
interface HeaderProps {
className: string;
title: ReactNode;
}
Header组件的用法如下。
<Header className='my-header' title={<Title color='red'> title of the page </Title>} />
现在在 Header 组件中,我想访问 Title 组件的颜色属性。
我正在考虑使用props.title.props.color。但是打字稿会抛出错误
props is not assignable to "whole ReactNode Thing"
【问题讨论】:
-
试试这个 ---> `title: ReactElement;`
-
哇...问题现在解决了。但我最近发现这样做是不好的做法。请在答案中添加此内容。我会接受的。
标签: javascript reactjs typescript react-props