有什么方法可以在react native中使用ref获取TextInput的文本值吗?

问题描述 投票:0回答:1

我正在使用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);
  }

但是实际上,每当更改文本时,由于状态值的更改也会调用render函数,这会使整个屏幕变慢。

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

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

希望我能提供帮助。

谢谢...

react-native textinput
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.