2

我正在为我的应用程序使用<InputText>of react-native-paper。每次我从一个视图切换到另一个视图时,我都会使用该autofocus属性,所以当我到达屏幕时,我可以直接输入。

但问题是键盘随机出现......有时键盘单独出现,有时我必须点击输入才能显示键盘......

这是我到处使用的输入示例:

<TextInput
                    label="Article"
                    value={this.state.article}
                    onChangeText={article => this.setState({article})}
                    onSubmitEditing={this.submitStepSaisie1}
                    autoFocus
                    style={{backgroundColor: 'transparent'}}
                />

我尝试在我的构造函数中使用这样的 ref :

constructor(props) {

    super(props);
    this.fieldOne = React.createRef();
    this.fieldTwo = React.createRef();
}

在我的 TextInput 上:

<TextInput ref={this.fieldOne} />

但是当我调用this.fieldOne.current.focus()它时,它会返回以下错误:Cannot read property 'focus' of null.

你们有窍门吗?谢谢 !

4

1 回答 1

0
import { TextInput as NativeTextInput } from 'react-native';

<TextInput
  label="Article"
  value={this.state.article}
  onChangeText={article => this.setState({article})}
  onSubmitEditing={this.submitStepSaisie1}
  autoFocus
  style={{backgroundColor: 'transparent'}}
  render={(props) => <NativeTextInput {...props} ref={this.fieldOne}>}
/>

我没有时间对此进行测试,但这可能会奏效。

于 2020-01-21T07:30:54.213 回答