Reactjs 针对自定义组件的React Native与Code Intellisense
我在理解如何制作定制组件时遇到了一些困难。我为自己制作了一个简单的文本组件,以避免每次使用文本时设置fontsize和fontfamilyReactjs 针对自定义组件的React Native与Code Intellisense,reactjs,react-native,visual-studio-code,Reactjs,React Native,Visual Studio Code,我在理解如何制作定制组件时遇到了一些困难。我为自己制作了一个简单的文本组件,以避免每次使用文本时设置fontsize和fontfamily import React, { Component } from 'react'; import { Colors } from "../assets/Colors" import { Text as RNText } from 'react-native'; class Text extends Component { re
import React, { Component } from 'react';
import { Colors } from "../assets/Colors"
import { Text as RNText } from 'react-native';
class Text extends Component {
render() {
return (
<RNText
{...this.props}
style={[{
fontFamily: 'Roboto-Medium',
fontSize: 16,
color: Colors.text
}, this.props.style]}>
{this.props.children}
</RNText>
)
}
}
export default Text;
import React,{Component}来自'React';
从“./assets/Colors”导入{Colors}
从“react native”导入{Text as RNText};
类文本扩展组件{
render(){
返回(
{this.props.children}
)
}
}
导出默认文本;
这里的问题是,当我键入自己的组件时,
“您需要在类组件中定义props type bypassprops
,就像这样扩展组件{
import React,{Component}来自'React';
从“./assets/Colors”导入{Colors}
从“react native”导入{文本作为RNText、StyleProp、ViewStyle};
类型道具={
样式:StyleProp
}
类文本扩展组件{
render(){
返回(
{this.props.children}
)
}
}
导出默认文本;
谢谢你的回答,但我得到了这个错误:类型参数只能在TypeScript文件中使用。ts(8011)我刚刚用这个网站解决了这个问题。但是现在我有一个Text.d.ts文件和Text.js文件。有没有办法在一个文件中解决这个问题?@SickRanchez你可以通过将文件扩展名js改为tsI来解决这个问题。我已经尝试过了,但它会带来很多错误。我还不太熟悉ts,也不知道语法应该是什么。我也找不到任何在线的conv从js到ts的RTER。我有点笨,那么你应该使用Text.d.ts方法
import React, { Component } from 'react';
import { Colors } from "../assets/Colors"
import { Text as RNText, StyleProp, ViewStyle } from 'react-native';
type Props = {
style: StyleProp<ViewStyle>
}
class Text extends Component<Props> {
render() {
return (
<RNText
{...this.props}
style={[{
fontFamily: 'Roboto-Medium',
fontSize: 16,
color: Colors.text
}, this.props.style]}>
{this.props.children}
</RNText>
)
}
}
export default Text;