【问题标题】:Newbie to React.js cannot determine proper Typescript InterfaceReact.js 的新手无法确定正确的 Typescript 接口
【发布时间】:2017-01-30 17:56:34
【问题描述】:
我是 trying this react example,我已将其放入 Visual Studio Code IDE。我在下面将此文件命名为 table.tsx。 react 的示例不包含我在下面放入的任何接口定义。
我必须将泛型(通过接口 defs)添加到 React.Component 类声明中。 React.Component 现在需要这个。那部分似乎奏效了。现在我在使用其余参数时遇到问题,如下所示...
colSpan下面有红线是因为不喜欢string参数?其他红线很可能是我没有正确设置道具界面的问题?
感谢任何见解。
【问题讨论】:
标签:
javascript
reactjs
visual-studio-code
【解决方案1】:
这里有几个问题。正如你所提到的,colSpan 应该是一个数字,而不是一个字符串,所以它需要看起来像这样:
<th colSpan={2}>
您还声明props.product 是一个数组,所以this.props.category 并没有多大意义。您可能想要的是 this.props[0].category 之类的东西,但您更可能希望将类别本身传递给 ProductCategoryRow 组件:
class ProductCategoryRow extends React.Component<{category:string},any>
您通常不希望将相同的道具形状传递给所有组件。您的 ProductCategoryRow 应该与您的 ProductRow 具有不同的道具。