[英]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.