在我看来,这是两个类似的设置,在一台机器上,我被允许打电话
<MyApp
accountId={this.props.accountId}
children={toggleButton} />
MyApp具有类签名的地方
export interface IMyAppProps {
accountId: string;
dispatch: any;
}
class MyApp extends React.Component<IMyAppProps, IMyAppState> {
在另一台计算机上,运行
npm run-script build
失败并显示两台机器上使用的版本均为react-scripts-ts 4.0.8,Typescript 3.4.5。
将类定义更改为此可以在两台计算机上使用
class MyApp extends React.Component<React.PropsWithChildren<IMyAppProps>, IMyAppState> {
我不确定为什么它可以在一种设置中起作用而在另一种设置中却不起作用。什么时候将props接口(interface)包装在
React.PropsWithChildren
中?我最好的猜测是更改将在React或与React相关的包的类型文件中找到。
最佳答案
我不知道为什么它可以在一台机器上运行,但不能在另一台机器上运行,但是我认为这不是使用children
属性的正确方法。
代替这个...
<MyApp
accountId={this.props.accountId}
children={toggleButton} />
...我认为children
的目的是让您调用这样的内容...<MyApp accountId={this.props.accountId}>
<ToggleButton/>
</MyApp>
... 或类似的东西。一个或多个 child (在我上面的示例中为
<ToggleButton/>
)可能是一个React元素,或者是文本(文本节点),如果没有 child ,则可能是undefined
,或者可能是像{toggleButton}
这样的表达式,其计算结果是其中一种子类型。