Search code examples
reactjsreact-nativereact-native-iosreact-animated

React Native - Hide and Show Component on Button Click with Animation


Friend I have issue to hide and show at runtime when user click on the button the the component (View) have to hide and again click on the button it should be show.

MYCODE :

  constructor(props) {
    super(props);
    this.state = {
      isModalVisible : false,
    };
  }

callFunc(){
   if(this.isModalVisible){
     this.setState({isModalVisible:false});
   }else{
     this.setState({isModalVisible:true});
   }
}

render() {
    return (

      <View style = {styles.companyScroll}>
         <Button
          onPress={this.callFunc}
          title="Learn More"
          color="#841584"
          accessibilityLabel="Learn more about this purple button"
         />

        {this.state.isModalVisible &&  <Picker style ={{backgroundColor : 'white'}}
                selectedValue={this.state.language}
                onValueChange={(itemValue, itemIndex) => this.setState({language: itemValue})}>
                <Picker.Item label="Java" value="java" />
                <Picker.Item label="JavaScript" value="js" />
              </Picker>

      </View>
   )
}

I want to like below image.

enter image description here


Solution

  • You should also use this.state.isModalVisible instead of this.isModalVisible from callFunc().