【问题标题】:Is there a any way to get the text value of the TextInput using ref in react native?有什么方法可以在 React Native 中使用 ref 获取 TextInput 的文本值?
【发布时间】:2020-09-04 01:29:36
【问题描述】:

我正在使用 onChangeText 事件和状态获取 TextInput 的文本。

      <View style={styles.inputContainerStyle}>
        <TextInput
          autoFoucs={true}
          ref={inputRef}
          style={styles.textInputStyle}
          placeholder="type here ..."
          placeholderTextColor='#838389'
          multiline
          underlineColorAndroid="transparent"
          onChangeText={text => handleTextChange(text)}
          value={selectedText}
        />
        <TouchableOpacity style={styles.submitBtnWrapperStyle} onPress={() => handleMsgSend()}>
          <Icon name={msgEditMode ? "check" : "paper-plane"} size={20} color="#ffffff" />
        </TouchableOpacity>
      </View>

...

// handle text change on textinput
  const handleTextChange = text => {
    channel && channel.typing();
    setSelectedText(text);
  }

但实际上每当文本改变时,渲染函数也会因为状态值的改变而被调用,这使得整个屏幕变得更慢。

所以我正在寻找不使用onChangeText 事件来获取文本的替代方法。

有没有使用 ref 获取文本的本地方法?

希望有帮助。

谢谢...

【问题讨论】:

  • 那么你打算在按下父级按钮时从文本输入中获取值吗?

标签: react-native textinput


【解决方案1】:

是的,您可以像这样使用 ref 访问文本输入的值

this.inputRef._lastNativeText

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 2023-03-26
    • 2021-09-20
    • 2020-05-22
    • 2021-08-19
    相关资源
    最近更新 更多