简体   繁体   English

在React Native中的其他.tsx组件中使用.tsx组件

[英]Using .tsx components in other .tsx components in React Native

I am teaching myself to build apps with TypeScript in React Native. 我正在自学在React Native中使用TypeScript构建应用程序。 As a Swift developer, JS and TS take a little bit of getting used to. 作为Swift开发人员,JS和TS习惯了一点。

One thing I noticed is that is seems impossible to use a component I wrote in a tsx file inside another tsx file in the Render method. 我注意到的一件事是,似乎不可能使用我在Render方法中另一个tsx文件中的tsx文件中编写的组件。

//SomeComponent.tsx

export default class SomeComponent extends Component {
    //all my logic
}

//OtherComponent.tsx
export default class ScoreTable extends Component {
    //logic
    render() {

      <SomeComponent style={{flex: 1}}></SomeComponent>
    }
}

This would give me the following error: 这将给我以下错误:

Type '{ style: { flex: number; }; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<SomeComponent> & Readonly<{}> & Readonly<{ children?: ReactNode; }>'.

I can resolve this issue by simply turning my tsx SomeComponent into a .js component, but I really like the tsx syntax. 我可以通过将tsx SomeComponent转换为.js组件来解决此问题,但是我真的很喜欢tsx语法。 So my question is Why can't I use .tsx components in other tsx components? 所以我的问题是,为什么我不能在其他tsx组件中使用.tsx组件? Or is there another way to do this? 还是有其他方法可以做到这一点?

You need to define style as a prop that your SomeComponent accepts: 您需要将style定义为SomeComponent接受的道具:

import React, { Component, CSSProperties } from "react";

interface Props {
  style: CSSProperties;
}

export default class SomeComponent extends Component<Props> {

I agree this error is confusing. 我同意这个错误令人困惑。

What's the problem? 有什么问题?

Essentially, this is due to not specifying the type for the Props of SomeComponent correctly, leading TypeScript to assume the bare minimum type definition, which does not include a style property. 本质上,这是由于未正确指定SomeComponentProps类型,导致TypeScript假定了最基本的最小类型定义,其中不包含style属性。

How do I fix it? 我如何解决它?

Add an interface for the props you expect to be accepted by SomeComponent, in much the same way as you might have previously done using PropTypes . 为您希望被SomeComponent接受的道具添加一个接口,其方式与您之前使用PropTypes所做的PropTypes

//SomeComponent.tsx

interface SomeComponentProps {
    style: React.CSSProperties;
}

export default class SomeComponent extends Component<SomeComponentProps> {
    //all my logic
}

How did you figure that out? 您是如何知道的?

There are a few clues. 有一些线索。 The first is the Type '{ style: { flex: number; }; }' 第一个是Type '{ style: { flex: number; }; }' Type '{ style: { flex: number; }; }' Type '{ style: { flex: number; }; }' part, which looks an awful lot like the attributes (aka props) you specify when using SomeComponent in OtherComponent.tsx . Type '{ style: { flex: number; }; }'部分,看起来极像是属性(又名道具)您在使用时指定SomeComponentOtherComponent.tsx So it probably has something to do with props for SomeComponent . 因此,它可能与SomeComponent道具有关。

The next part of the error says is not assignable to type , confirming that TypeScript thinks the type of the props doesn't match what it knows about SomeComponent . 错误的下一部分说is not assignable to type ,确认TypeScript认为道具的类型与它对SomeComponent了解SomeComponent

The final part of the error is the most confusing, where it lists the type 'IntrinsicAttributes & IntrinsicClassAttributes<SomeComponent> & Readonly<{}> & Readonly<{ children?: ReactNode; }>' 错误的最后一部分是最令人困惑的地方,它列出了类型'IntrinsicAttributes & IntrinsicClassAttributes<SomeComponent> & Readonly<{}> & Readonly<{ children?: ReactNode; }>' 'IntrinsicAttributes & IntrinsicClassAttributes<SomeComponent> & Readonly<{}> & Readonly<{ children?: ReactNode; }>' . 'IntrinsicAttributes & IntrinsicClassAttributes<SomeComponent> & Readonly<{}> & Readonly<{ children?: ReactNode; }>' Searching for IntrinsicAttributes in my React code allowed me to see that it was indeed to do with the base type of the attributes expected by a component (I found it in node_modules/@types/react/index.d.ts , the type definitions for react). 在React代码中搜索IntrinsicAttributes可以使我看到它确实与组件期望的属性的基本类型有关(我在node_modules/@types/react/index.d.ts找到了它的类型定义,反应)。

Combining all of these clues with the prior knowledge of how to strongly type the props and state of custom react components in TypeScript using the two optional generic type params to React.Component lead me to the final solution. 将所有这些线索与如何使用React.Component的两个可选泛型类型参数在TypeScript中强力键入React.Component和自定义react组件的状态的先验知识相结合,可以得出最终的解决方案。

Hopefully you now feel more empowered to decipher similarly confusing error messages in future. 希望您现在能更有能力解密将来同样令人困惑的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM