如何在react-native的自定义组件中自动完成样式?
<MyCustomComponent style={{AlignItems: 'center'}}>
例如AlignItems
和'center'
不会自动完成。
但是,如果我在View
中编写样式,它将自动完成。
<View style={{AlignItems: 'center'}}>
不,我不想使用Stylesheet.create()。
答案 0 :(得分:0)
style
仅在纯反应本机组件(例如View
,Text
等)中自动建议。但是当涉及到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> {
...
}
这将为您提供自动完成功能。