refs不能在react-native中工作

Tik*_*aba 6 reactjs react-native

我有一个TextInput,我想在我的函数中引用它.

next() {
        let body = this.refs.body.value
    }

<View>
    <Text>Place the body here</Text>
    <TextInput ref="body" placeholder="Your body goes here..." style={styles.body} placeholderTextColor='green'/>
</View>
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:

undefined不是对象(评估'this.refs.body')

ref不是在本机做反应?

Jef*_*cko 9

我认为他们已经改变了ref的工作方式.现在,ref不接受字符串,而是接受在呈现特定组件时调用的函数.
你可以试试像,

next() {
let body = this._textInput.value
}

<View>
    <Text>Place the body here</Text>
    <TextInput ref={component => this._textInput = component} placeholder="Your body goes here..." style={styles.body} placeholderTextColor='green'/>
</View>
Run Code Online (Sandbox Code Playgroud)

https://facebook.github.io/react-native/docs/direct-manipulation.html

或者,您也可以将onChange附加到TextInput,并在单击下一个按钮时记录输入.

编辑:
Ref仍然接受字符串,但它将被弃用.请改用ref中的函数.