javascript - 未设置本机基本输入参考
问题描述
所以我正在使用 Native Base<Input>
标记,试图将 refs 设置为通过表单字段处理“tabbing”。我有以下代码:
<Item floatingLabel>
<Label style={{ color: "#fff" }}>First Name</Label>
<Input
ref={input => {
this.firstNameRef = input;
}}
onSubmitEditing={() => {
this.lastNameRef._root.focus();
}}
returnKeyType={"next"}
/>
</Item>
<Item floatingLabel last>
<Label style={{ color: "#fff" }}>Last Name</Label>
<Input
ref={input => {
this.lastNameRef = input;
}}
/>
</Item>
所以基本上,我有 2 个<Input>
标签,都带有ref
set (this.firstNameRef
和this.lastNameRef
),但是在加载应用程序时,按 First Name,然后按键盘上的“next”,我收到以下错误:
无法读取未定义
onSubmitEditing
Signup.js:162:26的属性“_root”
似乎 using<Input>
实际上并没有设置任何 ref 值,this.lastNameRef
所以null
.
我还尝试使用 React Native 的<TextInput>
元素,它确实如上所述处理设置 refs,但在提交后似乎仍然没有处理焦点(即使._root
从.focus()
逻辑中删除)。
有没有其他人看到这个问题?
注意:目前仅在 iOS 中测试。
解决方案
已接受答案的更新:ref
两者getRef
都有效,但只有一个可以依赖包装组件。在这种情况下,我<Input>
的组件由一个<Item>
组件包裹,每个组件都有不同的label
属性。比较:
<Item floatingLabel>
<Label>First Name</Label>
<Input
getRef={input => {
this.firstNameRef = input;
}}
onSubmitEditing={() => {
this.lastNameRef._root.focus();
}}
returnKeyType={"next"}
/>
</Item>
和
<Item fixedLabel>
<Label>First Name</Label>
<Input
ref={input => {
this.lastNameRef = input;
}}
onSubmitEditing={() => {
this.lastNameRef._root.focus();
}}
returnKeyType={"next"}
/>
</Item>
在 的情况下floatingLabel
,getRef
有效,而ref
无效。另一方面,在 的情况下fixedLabel
,ref
有效,而getRef
无效。
并不是真的要解释为什么,但也许这个警告会在未来帮助其他人。
推荐阅读
- c# - 将 2D 数组拆分为 4 个较小的 2D 数组
- flutter - Flutter:并非所有本地化代表都支持应用程序的语言环境
- scala - 如何在宏中获取 Scala-macro-annotated 类或对象的扩展类路径?
- sapui5 - SAPUI5中匹配无效哈希时如何显示NotFound
- html - 如何使网站垂直响应?
- c# - Tcp Listener 和 Client 只接收和发送一次
- c - 在c中比较两个具有相同名称的不同字符串
- clojure - 在 Clojure 中,可以在使用顺序解构时提供默认值吗?
- scala - 找不到符号符号:变量参数位置:com.twitter.finagle.transport.Transport.Liveness 类
- r - 将数据从本地目录移动到 AWS