【发布时间】:2021-09-29 08:02:55
【问题描述】:
所以,我正在使用 React Native,之前从未使用过样式化组件。
当我创建一个没有 SC 的新组件时,例如自定义按钮,我执行以下操作:
import React, { ReactNode } from 'react'
import { TouchableOpacity, TouchableOpacityProps} from 'react-native'
import styles from './styles'
type CustomButtonProps = TouchableOpacityProps & {
children: ReactNode
}
const CustomButton = ({ children, ...props }: CustomButtonProps) => {
return (
<TouchableOpacity style={styles.container} {...props}>
{children}
</TouchableOpacity>
)
}
export { CustomButton }
这样,当我在其他地方使用我的组件时,我可以传递所有未在我的自定义道具中列出的附加 TouchableOpacity 道具。
我想知道当我使用 Styled Components 以继续使用未列出的道具时,我使用什么类型来替换 TouchableOpacityProps,因为如果我使用 TouchableOpacityProps,它会告诉我一个错误类型不匹配:
import React, { ReactNode } from 'react'
import { StyledTouchableOpacity } from './styles'
type CustomButtonProps = ??? & {
children: ReactNode
}
const CustomButton = ({ children, ...props }: CustomButtonProps) => {
return (
<StyledTouchableOpacity {...props}>
{children}
</StyledTouchableOpacity>
)
}
export { CustomButton }
谢谢你:)
【问题讨论】:
标签: typescript react-native styled-components react-typescript