在 React Native 中隐藏/显示组件 [英] Hide/Show components in react native
问题描述
我真的是 React Native 的新手,我想知道如何隐藏/显示组件.
这是我的测试用例:
I'm really new to React Native and I'm wondering how can I hide/show a component.
Here's my test case:
<TextInput
onFocus={this.showCancel()}
onChangeText={(text) => this.doSearch({input: text})} />
<TouchableHighlight
onPress={this.hideCancel()}>
<View>
<Text style={styles.cancelButtonText}>Cancel</Text>
</View>
</TouchableHighlight>
我有一个 TextInput
组件,我想要的是在输入获得焦点时显示 TouchableHighlight
,然后隐藏 TouchableHighlight
用户按下取消按钮.
I have a TextInput
component, what I want is to show the TouchableHighlight
when the input gets the focus, then hide the TouchableHighlight
when the user press the cancel button.
我不知道如何访问"TouchableHighlight
组件,以便在我的函数 showCancel/hideCancel
中隐藏/显示它.
另外,如何从一开始就隐藏按钮?
I don´t know how to "access" the TouchableHighlight
component in order to hide/show it inside of my functions showCancel/hideCancel
.
Also, how can I hide the button from the very beginning?
推荐答案
我会这样做:
var myComponent = React.createComponent({
getInitialState: function () {
return {
showCancel: false,
};
},
toggleCancel: function () {
this.setState({
showCancel: !this.state.showCancel
});
}
_renderCancel: function () {
if (this.state.showCancel) {
return (
<TouchableHighlight
onPress={this.toggleCancel()}>
<View>
<Text style={styles.cancelButtonText}>Cancel</Text>
</View>
</TouchableHighlight>
);
} else {
return null;
}
},
render: function () {
return (
<TextInput
onFocus={this.toggleCancel()}
onChangeText={(text) => this.doSearch({input: text})} />
{this._renderCancel()}
);
}
});
这篇关于在 React Native 中隐藏/显示组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!