Hide/Show components in react native

后端 未结 24 1960
囚心锁ツ
囚心锁ツ 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:15

    I had the same issue where I would want to show / hide Views, but I really didn't want the UI jumping around when things were added/removed or necessarily to deal with re-rendering.

    I wrote a simple Component to deal with it for me. Animated by default, but easy to toggle. I put it on GitHub and NPM with a readme, but all the code is below.

    npm install --save react-native-hideable-view

    import React, { Component, PropTypes } from 'react';
    import { Animated  } from 'react-native';
    
    class HideableView extends Component {
      constructor(props) {
        super(props);
        this.state = {
          opacity: new Animated.Value(this.props.visible ? 1 : 0)
        }
      }
    
      animate(show) {
        const duration = this.props.duration ? parseInt(this.props.duration) : 500;
        Animated.timing(
          this.state.opacity, {
            toValue: show ? 1 : 0,
            duration: !this.props.noAnimation ? duration : 0
          }
        ).start();
      }
    
      shouldComponentUpdate(nextProps) {
        return this.props.visible !== nextProps.visible;
      }
    
      componentWillUpdate(nextProps, nextState) {
        if (this.props.visible !== nextProps.visible) {
          this.animate(nextProps.visible);
        }
      }
    
      render() {
        if (this.props.removeWhenHidden) {
          return (this.visible && this.props.children);
        }
        return (
          <Animated.View style={{opacity: this.state.opacity}}>
            {this.props.children}
          </Animated.View>
        )
      }
    }
    
    HideableView.propTypes = {
      visible: PropTypes.bool.isRequired,
      duration: PropTypes.number,
      removeWhenHidden: PropTypes.bool,
      noAnimation: PropTypes.bool
    }
    
    export default HideableView;
    
    0 讨论(0)
  • 2020-12-07 09:16

    React Native's layout has the display property support, similar to CSS. Possible values: none and flex (default). https://facebook.github.io/react-native/docs/layout-props#display

    <View style={{display: 'none'}}> </View>
    
    0 讨论(0)
  • 2020-12-07 09:17

    The following example is coding in typescript with Hooks.

    import React, { useState, useEffect } from "react";
    
    ........
    
    const App = () => {
    
       const [showScrollView, setShowScrollView] = useState(false);
    
       ......
    
       const onPress = () => {
        // toggle true or false
        setShowScrollView(!showScrollView);
      }
    
      ......
    
          </MapboxGL.ShapeSource>
            <View>{showScrollView ? (<DetailsScrollView />) : null}</View>
          </MapboxGL.MapView>
      ......
    
    }
    
    0 讨论(0)
  • 2020-12-07 09:17

    Very Easy. Just change to () => this.showCancel() like below:

    <TextInput
            onFocus={() => this.showCancel() }
            onChangeText={(text) => this.doSearch({input: text})} />
    
    <TouchableHighlight 
        onPress={this.hideCancel()}>
        <View>
            <Text style={styles.cancelButtonText}>Cancel</Text>
        </View>
    </TouchableHighlight>
    
    0 讨论(0)
  • 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(
                   <TouchableHighlight
                       style={{borderColor:'black',borderWidth:1,marginTop:20}}
                       onPress={this.hideCancel}>
                       <View>
                           <Text>Cancel</Text>
                       </View>
                   </TouchableHighlight>
               )
            }
            return null;
        }
    
        render() {
    
    
            return (
                <View style={{justifyContent:'center',alignItems:'center',flex:1}}>
                    <TextInput
                        style={{borderColor:'black',borderBottomWidth:1}}
                        onFocus={this.showCancel}
                    />
                    {this.renderTouchableHighlight()}
    
                </View>
            );
        }
    }
    
    export default App;
    

    0 讨论(0)
  • 2020-12-07 09:19

    Most of the time i'm doing something like this :

    class MyComponent extends React.Component {
      constructor(props) {
        super(props);
        this.state = {isHidden: false};
        this.onPress = this.onPress.bind(this);
      }
      onPress() {
        this.setState({isHidden: !this.state.isHidden})
      }
      render() {
        return (
          <View style={styles.myStyle}>
    
            {this.state.isHidden ? <ToHideAndShowComponent/> : null}
    
            <Button title={this.state.isHidden ? "SHOW" : "HIDE"} onPress={this.onPress} />
          </View>
        );
      }
    }
    

    If you're kind of new to programming, this line must be strange to you :

    {this.state.isHidden ? <ToHideAndShowComponent/> : null}
    

    This line is equivalent to

    if (this.state.isHidden)
    {
      return ( <ToHideAndShowComponent/> );
    }
    else
    {
      return null;
    }
    

    But you can't write an if/else condition in JSX content (e.g. the return() part of a render function) so you'll have to use this notation.

    This little trick can be very useful in many cases and I suggest you to use it in your developments because you can quickly check a condition.

    Regards,

    0 讨论(0)
提交回复
热议问题