自定义组件中的本机样式自动完成

时间:2020-02-24 15:17:19

标签: react-native visual-studio-code

如何在react-native的自定义组件中自动完成样式?

<MyCustomComponent style={{AlignItems: 'center'}}>

例如AlignItems'center'不会自动完成。

但是,如果我在View中编写样式,它将自动完成。

<View style={{AlignItems: 'center'}}>

不,我不想使用Stylesheet.create()。

2 个答案:

答案 0 :(得分:0)

style仅在纯反应本机组件(例如ViewText等)中自动建议。但是当涉及到CustomComponent时,React本机无法像这样处理,因为您可以自由使用任何名称,因此您可以将任何道具传递给它。 例如。您可以将样式作为styleObject或仅作为字符串传递,因为它是您自己的组件,因此自动建议在自定义组件中不起作用

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

<CustomComp style="Bring me a pizza"  />

希望您能掌握要点。随时有疑问

答案 1 :(得分:0)

如果您使用打字稿或Flow,则可以键入组件属性并根据需要获取自动完成功能

interface IMyCustomComponentProps {
  style?: ViewStyle | TextStyle | ...
}

export class MyCustomComponent extends PureComponent<IMyCustomComponentProps> {
...
}

这将为您提供自动完成功能。