Hide/Show components in react native

后端 未结 24 2027
囚心锁ツ
囚心锁ツ 2020-12-07 08:43

I\'m really new to React Native and I\'m wondering how can I hide/show a component.
Here\'s my test case:



        
24条回答
  •  借酒劲吻你
    2020-12-07 09:17

    The only way to show or hide a component in react native is checking a value of a parameter of app state like state or props. I provided a complete example as below:

    import React, {Component} from 'react';
    import {View,Text,TextInput,TouchableHighlight} from 'react-native'
    
    class App extends Component {
    
        constructor(props){
            super(props);
            this.state={
                show:false
            }
    }
    
        showCancel=()=>{
            this.setState({show:true})
        };
    
        hideCancel=()=>{
            this.setState({show:false})
        };
    
        renderTouchableHighlight(){
            if(this.state.show){
               return(
                   
                       
                           Cancel
                       
                   
               )
            }
            return null;
        }
    
        render() {
    
    
            return (
                
                    
                    {this.renderTouchableHighlight()}
    
                
            );
        }
    }
    
    export default App;
    

提交回复
热议问题