如何使用useRef获取TextInput值?

hak*_*kan 6 react-native

我尝试获取 TextInput 的值

使用引用

但它给出了未定义的。这是我的代码:

const inputRef = useRef(null);
 <TextInput
     ref={inputRef}
     value={'test'}
     editable={false}
     style={styles.InputStyle}
 />
console.log(inputRef.current.value)  --->   gives undefined
Run Code Online (Sandbox Code Playgroud)

Kul*_*tel -3

使用函数设置引用,如下所示:

<TextInput ref={(ref) => { this.inputRef= ref; }} />
Run Code Online (Sandbox Code Playgroud)

然后您可以通过以下方式访问参考this.inputRef