如何在反应本机代码中获取选项卡对象

时间:2016-11-03 09:55:38

标签: react-native

<View style={styles.container}>
    <TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1}}
               onChangeText={(text) => this.setState({text})}
               value={this.state.text}/>
    <TouchableHighlight underlayColor='white' onPress={onPressButton}>
        <Image style={{width: 50, height: 50}}
               source={{uri: 'https://facebook.github.io/react/img/logo_og.png'}}
        />
    </TouchableHighlight>
</View>

我想获取View,就像在html中使用'getElementById()'一样,但在React native中,我该怎么做?

1 个答案:

答案 0 :(得分:1)

定义ref

<View ref={(ref) => {this._myView = ref}} />

然后在您的代码中,您可以像这样访问View

this._myView  // Do whatever you need here

此处的文档https://facebook.github.io/react/docs/refs-and-the-dom.html#the-ref-callback-attribute