Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide/Show components in 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> 

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.

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?

like image 403
Crysfel Avatar asked May 15 '15 18:05

Crysfel


People also ask

How do you hide and show components in React Native?

to display 'show' if show is true and 'hide' otherwise. We control the value of show with the toggle button. To change it, we set onPress to a function that calls setShow with a callback to toggle the show value. Therefore, when we press the button, we see the text toggle between 'show' and 'hide'.

How do I hide an element in React Native?

You can wrap whatever component you are trying to hide with a View with style={{width:0, height:0}}.


1 Answers

In your render function:

{ this.state.showTheThing &&    <TextInput/> } 

Then just do:

this.setState({showTheThing: true})  // to show it   this.setState({showTheThing: false}) // to hide it 
like image 126
Krishan Gupta Avatar answered Sep 22 '22 01:09

Krishan Gupta