简体   繁体   English

如何扩展本机组件打字稿

[英]How to extend react-native component typescript

I am trying to compose text components to use throughout a react native application 我正在尝试编写文本组件以在整个React Native应用程序中使用

it is as follows 如下

import React from 'react';
import { Text, TextProperties, StyleSheet } from 'react-native';
import Colors from '../Colors';

export default (props: TextProperties) => <Text style={styles.text} {...props} />

const styles = StyleSheet.create({
    text: {
        color: Colors.primary,
        fontSize: 24,
        fontWeight: 'bold'
    }
});

In effect I am trying to apply styles in a single location and use it throughout the applicaiton. 实际上,我试图在单个位置应用样式,并在整个应用程序中使用它。

my issue is that the typescript compiler is complaining: 我的问题是打字稿编译器抱怨:

 error TS2559: Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes & TextProperties'.

I figured out a solution. 我想出了一个解决方案。 What I did was. 我所做的是。

import React from 'react';
import { Text, TextProperties, StyleSheet } from 'react-native';
import Colors from '../Colors';

export default (props: Props) => <Text style={styles.text} {...props} />

interface Props extends TextProperties {
   children: string;
}
const styles = StyleSheet.create({
    text: {
        color: Colors.primary,
        fontSize: 24,
        fontWeight: 'bold'
    }
});

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

相关问题 Typescript扩展反应原生View组件 - Typescript extend react native View component 如何在React Native的无状态功能组件中扩展Native组件的props TypeScript接口? - How to extend a native's component's props TypeScript interface in a stateless, functional component in React Native? 如何为 react-native 本机 ui 组件创建打字稿类型定义? - How do I create typescript type definitions for react-native native ui component? 如何使用带有TypeScript的React-Native中的样式组件键入无状态功能组件? - How to type Stateless Functional Component using styled-component in React-Native with TypeScript? React 和 Typescript:如何扩展通用组件道具? - React and Typescript: How to extend generic component props? 如何使用 Yarn 和 Typescript 设置 React-Native - How to setup React-Native with Yarn and Typescript React-native:样式组件和 typescript 中的多个样式道具 - React-native: multiple style props in styled-component and typescript React-native & typescript:有条件地将组件分配给变量 - React-native & typescript: Conditionally assign component to variable Typescript React-native 样式组件中的类型问题 - Type issue in Typescript React-native styled-component Typescript 样式组件按钮中的问题 ( React-Native ) - Typescript issue in Styled component Button ( React-Native )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM