【发布时间】:2020-09-12 04:48:36
【问题描述】:
我正在创建一个自定义组件,脚本是使用 React 的 Hooks 用 TypeScript 编写的
我的组件将使用我的一些打字稿类型并将其与 TextInput 类型混合(这样我就可以访问 TextInput 道具)
这是我的自定义组件示例代码
import { Animated, Platform, TextInput, TextInputProps, View } from 'react-native'
type PropsType = TextInputProps & { //Or should I using TextInput instead of TextInputProps?
myCustomProp?: string
}
const CustomTextField = (props: PropsType) => {
useEffect(() => {
return () => {
if(props.onSubmitEditing != undefined) {
props.onSubmitEditing()
}
}
}, [])
return (
<View
style = {{
}}
/>
)
}
export default CustomTextField
然后我将它导入到我的屏幕上并像这样显示它
<CustomTextField
placeholder = 'Password'
ref = {passwordInput}
secureTextEntry
/>
这是我的参考变量
const passwordInput = useRef<typeof CustomTextField>(null)
但是当引用组件时打字稿给我错误
【问题讨论】:
标签: typescript react-native react-hooks