简体   繁体   English

如何使用 React Native 中的引用更改 TextInput 的值?

[英]How to change the value of a TextInput using reference in React Native?

I am using React Native 0.57.8 and React 16.7.0 .我正在使用 React Native 0.57.8和 React 16.7.0 I am creating an on-screen keyboard for Android TV which will be used as a library.我正在为 Android TV 创建一个屏幕键盘,它将用作库。 I have a TextInput to whom I have assigned a reference.我有一个TextInput ,我已经为其分配了一个引用。 How can I use this reference to change the value of the TextInput ?如何使用此引用来更改TextInputvalue

constructor(props) {
  super(props);
  this.emailRef = React.createRef();
}

<TextInput
  ref={this.emailRef}
  placeHolder="Email Address"
  blurOnSubmit={false}
/>

<Keyboard textInput={this.emailRef} />

Inside the library:图书馆内部:

<Button
  text="change value"
  onPress={() => {
    this.props.emailRef.current.props.value =
      this.props.emailRef.current.props.value + "q";
  }}
/>

I think what you need is a controlled input.我认为您需要的是受控输入。 Here is how I would do that:这是我将如何做到这一点:

constructor(props) {
  super(props);
  this.emailRef = React.createRef(); // might not need this
  this.state = {
    value: ''
  }
}

<TextInput
  value={this.state.value}
  onChangeText={(text) => this.setState({text})}
  placeHolder="Email Address"
  blurOnSubmit={false}
/>

<Button
  text="change value"
  onPress={() => {
    // just use `this.state.value` to do whatever you need with it
  }}
/>

You can't change a prop directly within the component - Props can only be derived from a parent component, but cannot be modified, so you can't do:您不能直接在组件内更改道具 - 道具只能从父组件派生,但不能修改,因此您不能这样做:

this.props.emailRef.current.props.value = this.props.emailRef.current.props.value + "q";

Also, you reference this.props.emailRef in your library, whereas the keyboard doesn't have the emailRef prop - it has the textInput prop.此外,您在库中引用this.props.emailRef ,而键盘没有emailRef道具 - 它有textInput道具。

Try this:尝试这个:

constructor(props) {
  super(props);
  this.emailRef = React.createRef();
  this.state = {
    value: "Initial Input"
  };
  this.handleInput = this.handleInput.bind(this);
}

handleInput(input) {
  this.setState({ value: input });
}

render() {
  return (
    ...
      <Keyboard textInput={this.emailRef} onInput={this.handleInput} />
      <TextInput ref={this.emailRef} value={this.state.value} />
    ...
  );
}

Inside the library:图书馆内部:

<Button
  text="change value"
  onClick={() => {
    this.props.onInput(this.props.textInput.current.props.value + "q");
  }}
/>

Set text in state method then update state in pressed button在状态方法中设置文本,然后在按下的按钮中更新状态

then set in text like this然后像这样设置文本

<Text>
       {this.state.myText}
    </Text>

Use state they all say without knowing why I exactly need to update UI without using state.使用状态他们都说不知道为什么我需要在不使用状态的情况下更新 UI。 In my case the text input was using state, and when typed really fast, the asynchronous state and UI update lags behind the speed of typing, and cursor lags couple of letters behind causing in errors in typing.在我的例子中,文本输入使用状态,当输入非常快时,异步状态和 UI 更新滞后于输入速度,光标滞后几个字母,导致输入错误。 Only way to prevent this is not to use any state!防止这种情况的唯一方法是不使用任何状态! if you do not use state, you cannot give text input an initial value without making it readonly.如果不使用状态,则不能在不将其设为只读的情况下为文本输入提供初始值。 To give TextInput an initial value, we can use ref and set native props programmatically upon component mount event.为了给 TextInput 一个初始值,我们可以使用 ref 并在组件安装事件上以编程方式设置本机道具。 like so:像这样:

const setInitialBio = React.useCallback(() => {
    if(inputRef.current) {
        inputRef.current.setNativeProps({ text: "initial value goes here" })
    }
}, []);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM