【发布时间】:2018-10-21 08:03:13
【问题描述】:
我正在使用带有 react native 的 typescript,我不知道我是否正确定义了我的输入引用类型。
这是我代码的相关部分:(两个组件都是我的自定义组件,TextInput 不适用于 react-native。)
// in begin of class
private readonly passwordInputRef: React.RefObject<Password> = createRef();
// in render function
<TextInput
key={'login page username'}
language={language}
placeholder={'Username:'}
value={username}
containerStyle={styles.username}
hasError={loginError}
onChangeText={this.setUsername}
returnKeyType={"next"}
blurOnSubmit={false}
onSubmitEditing={() => this.passwordInputRef.current!.focus()} />
<Password
key={'login page password'}
ref={this.passwordInputRef}
language={language}
placeholder={'Password:'}
value={password}
containerStyle={styles.password}
hasError={loginError}
onChangeText={this.setPassword}/>
当我运行应用程序并按下键盘上的下一步按钮时,它给了我一个错误_this.passwordInputRef.current.focus is not a function
我在 stackoverflow 上搜索并阅读了许多答案,但没有一个没有解释它是如何与 typescript 一起工作的。
我也读过这个issue on github,似乎我的代码与那个非常相似,除了我使用的是打字稿,所以我认为我对passwordInputRef的定义命令有问题。
我做错了什么?
目前我正在使用:
"react": "16.5.2"
"react-native": "0.55.2"
【问题讨论】: