【发布时间】:2020-07-22 22:04:54
【问题描述】:
我想从主题文件夹中导入一个可重用的 Button 组件。
这是Button组件的路径:
\app\theme\components\Button.ts
这是Button.ts的代码
import { typography } from "theme/typography";
import { ButtonProps } from "react-native-elements";
export const Button = {
titleStyle: {
fontSize: typography.size,
fontFamily: typography.primaryMedium,
lineHeight: typography.size,
letterSpacing: 0,
marginHorizontal: typography.size
},
containerStyle: {
borderRadius: typography.size * 2 + typography.size * 2
},
buttonStyle: {
borderRadius: typography.size * 2 + typography.size
}
} as ButtonProps
这是我要导入的组件:organizationdetails-screen.tsx。
它的路径是\app\screens\superadmin-screens\organizationdetails-screen.tsx。
我正在尝试像这样导入按钮
import { Button } from '../../theme/components/Button';
但是我收到了这个错误
我正在使用这样的按钮:
<Button
title="Add User"
onPress={() => this.props.navigation.navigate('AddNewUser')}
/>
当我将鼠标悬停在 Visual Studio Code 中的按钮上时,它会显示以下错误:
如何导入这个可重复使用的按钮?
【问题讨论】:
-
从'../../theme/components/Button'导入按钮作为导入按钮;移除 {}
-
我试过了,但它不起作用。它显示了这个错误i.ibb.co/ZN77T4S/error3.jpg
-
您的
Button不是组件。export const Button = { titleStyle: { fontSize: typography.size,位看起来像一个样式对象:您需要从您的Button返回一个反应元素,例如export const Button = () => <Text>I'm not a button</Text>- 当然使用您想要的道具、样式和元素。
标签: android typescript react-native