4

I am still quite new to react and till now I am still not able to understand how to remove a View when the user clicks on a specific button. For example lets say I have this block of code:

import React, { Component } from 'react'
import { AppRegistry, View, Text, TouchAbleOpacity } from 'react-native'

class Example extends Component {
    removeView(){
        // what should I do here?
    }

    render(){
        return (
            <View>
                <View> // View to be removed
                    <TouchAbleOpacity onPress={() => this.removeView()}>
                      <Text>Remove View</Text>
                    </TouchAbleOpacity>
                </View>
            </View>
        )
    }
}

AppRegistry.registerComponent('example', () => Example);

How do I programmatically remove the View element?

2 Answers 2

5

Try something like this:

class Example extends Component {
    constructor(props) {
      super(props);
      this.state = {
        showView: true,
      }
    }

    removeView(){
       this.setState({
         showView: false,
       });
    }

    render(){
        return (
           <View style={{flex: 1, alignItems: 'center', justifyContent: 'center' }}>
           {this.state.showView && (
                <View style={{backgroundColor: 'red', height: 100, width: 100 }}> // View to be removed
                    <TouchableHighlight onPress={() => this.removeView()}>
                      <Text>Remove View</Text>
                    </TouchableHighlight>
                </View>
           )}
           </View>
        )
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

How it will work for multiple View and TouchableHighlight? state is for common!
2

I do it like this:

import React, { Component } from 'react'
import { AppRegistry, View, Text, TouchAbleOpacity } from 'react-native'

class Example extends Component {

    constructor() {
      super();
      this.state = {
        showView:true
      }
    }

    removeView(){
        this.setState({
          showView:false
        })
    }

    render(){
        return (
            <View>
                {this.renderView()}
            </View>
        )
    }

    renderView(){
      if(this.state.showView){
        return(
          <View>
              <TouchAbleOpacity onPress={() => this.removeView()}>
                <Text>Remove View</Text>
              </TouchAbleOpacity>
          </View>
        );
      }
    }
}

AppRegistry.registerComponent('example', () => Example);

5 Comments

Is it 100% removed though? Or is it like adding a "hide class into a div"? also, will it work for multiple views?
Yes the View gets unmounted and yes you can do this for multiple views.
You're basically showing all the views if showView is set to true, so when the user clicks on the button wouldn't that remove all of the views?
Sorry I misunderstood your question with the multiple views. That is a good question by the way. I will need to think how I will do that never had to do it like that before.
@Alex, I ran into the same problem today. I had to remove buttons. So I created a new question on how to remove multiple components.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.