【问题标题】:React-Native refs undefined on text inputReact-Native refs 在文本输入上未定义
【发布时间】:2016-11-02 19:47:36
【问题描述】:

我们目前正在运行 React-Native 0.33

当他们点击下一个按钮时,我们正在尝试使用 refs 从 1 个文本输入转到另一个。经典的用户名到密码。

有人知道吗?下面是我们正在使用的代码。从我在堆栈上发现的其他帖子中,这就是他们所做的;但是,它告诉我们 this.refs 是未定义的。

更新 所以我把问题缩小到了

 render() {
    return (
      <Navigator
          renderScene={this.renderScene.bind(this)}
          navigator={this.props.navigator}

          navigationBar={
            <Navigator.NavigationBar style={{backgroundColor: 'transparent'}}
                routeMapper={NavigationBarRouteMapper} />
          } />
    );
  }

如果我只是在原始渲染内的 renderScene 函数中渲染下面的代码,它可以工作,但是使用导航器它将无法工作。有谁知道为什么?或者如何让导航器显示以及渲染renderScene中的代码以出现在原始渲染中?

   class LoginIOS extends Component{

  constructor(props) {
    super(props);
    this.state={
      username: '',
      password: '',
      myKey: '',
    };
  }

  render() {
    return (
      <Navigator
          renderScene={this.renderScene.bind(this)}
          navigator={this.props.navigator}

          navigationBar={
            <Navigator.NavigationBar style={{backgroundColor: 'transparent'}}
                routeMapper={NavigationBarRouteMapper} />
          } />
    );
  }

  renderScene() {
    return (
      <View style={styles.credentialContainer}>
                <View style={styles.inputContainer}>
                  <Icon style={styles.inputPassword} name="person" size={28} color="#FFCD00" />
                      <View style={{flexDirection: 'row', flex: 1, marginLeft: 2, marginRight: 2, borderBottomColor: '#e0e0e0', borderBottomWidth: 2}}>
                        <TextInput 
                            ref = "FirstInput"
                            style={styles.input}
                            placeholder="Username"
                            autoCorrect={false}
                            autoCapitalize="none"
                            returnKeyType="next"
                            placeholderTextColor="#e0e0e0"
                            onChangeText={(text) => this.setState({username: text})}
                            value={this.state.username}
                            onSubmitEditing={(event) => {
                              this.refs.SecondInput.focus();
                            }}
                            >

                        </TextInput>
                      </View>
                </View>

                <View style={styles.inputContainer}>
                  <Icon style={styles.inputPassword} name="lock" size={28} color="#FFCD00" />
                      <View style={{flexDirection: 'row', flex: 1, marginLeft: 2, marginRight: 2, borderBottomColor: '#e0e0e0', borderBottomWidth: 2}}>
                        <TextInput
                            ref = "SecondInput"
                            style={styles.input}
                            placeholder="Password"
                            autoCorrect={false}
                            secureTextEntry={true}
                            placeholderTextColor="#e0e0e0"
                            onChangeText={(text) => this.setState({password: text})}
                            value={this.state.password}
                            returnKeyType="done"
                            onSubmitEditing={(event)=> {
                              this.login();
                            }}
                            focus={this.state.focusPassword}
                            >
                        </TextInput>
                      </View>
                </View>
      </View>
      );
  }

【问题讨论】:

  • 只是尝试解决问题:尝试在构造函数中绑定渲染场景函数。所以添加以下行:this.renderScene = this.renderScene.bind(this)
  • 运气不好。尝试使用构造函数中的绑定以及它在渲染renderScene = {this.renderScene.bind(this)}中的位置。然后就在构造函数中试了一下。
  • @AlexHarrison 我发现它与渲染场景有关。如果我删除导航栏并只返回原始渲染中的输入,它就可以工作;但是,我需要导航栏。有什么想法吗?

标签: user-interface reactjs react-native


【解决方案1】:

尝试使用函数设置引用。像这样:

<TextInput ref={(ref) => { this.FirstInput = ref; }} />

然后您可以使用this.FirstInput 而不是 this.refs.FirstInput 访问引用

【讨论】:

【解决方案2】:

尝试将 Navigator 的 renderScene 回调更改为以下(基于Navigator 文档),因为稍后您将需要导航器对象。

renderScene={(route, navigator) => this.renderScene(route, navigator)}

然后,使用 'navigator' 而不是 'this' 来获取 refs。

navigator.refs.SecondInput.focus()

【讨论】:

  • 我也试过了,但没有运气:/
  • 嗯,不确定,但您是否修改了 renderScene() 函数以接收 'route' 和 'navigator' 对象?
【解决方案3】:

对于使用useRef 挂钩的functional 组件。您可以使用...轻松实现此目的...

import React, { useRef } from 'react';
import { TextInput, } from 'react-native';

function MyTextInput(){
  const textInputRef = useRef<TextInput>(null);;

  return (
    <TextInput ref={textInputRef}  />
  )
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 2016-12-01
    • 2019-06-03
    • 2021-07-11
    相关资源
    最近更新 更多