Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove or unmount a component in React Native?

Tags:

react-native

I want to remove a component in my React Native code, just like the "el.parentNode.removeChild(el)" in JavaScript or "[view removeFromSuperview]" in Objective-C, but I didn't see any related API in React documents. Is there any way to make it?

like image 654
huimin Avatar asked May 14 '15 15:05

huimin


People also ask

How do you unmount component in react native?

ReactJS componentWillUnmount() Method The componentWillUnmount() method allows us to execute the React code when the component gets destroyed or unmounted from the DOM (Document Object Model). This method is called during the Unmounting phase of the React Life-cycle i.e before the component gets unmounted.

How do you unmount a component in React hooks?

Use the useEffect hook to run a react hook when a component unmounts. The function we return from the useEffect hook gets invoked when the component unmounts and can be used for cleanup purposes. Copied!

Which of the below methods helps in unmounting a component from the DOM?

componentWillUnmount. The componentWillUnmount method is called when the component is about to be removed from the DOM.

How do I unmount screen in react native?

This article will demonstrate via examples how to resolve the How To Unmount Inactive Screens In Bottom Tab React Native error . You can unmount screens in bottom tab by adding option in navigation screenOptions: unmountOnBlur: true You can do it in Tab & Drawer Navigations but not in Stack Navigation.


1 Answers

In React Native or generally in React as I understand you normally don't remove components by calling 'remove[..]' but by changing the markup of the component thus changing the virtual DOM.

Here's some sample code that removes a MapView of a screen.

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  SwitchIOS,
  MapView,
} = React;

var TestMap = React.createClass({

  getInitialState() {
    return {
      showMap: true,
    };
  },

  render: function() {
    var map = this.state.showMap ? <MapView style={styles.map}/> : null;
    return (
      <View style={styles.container}>
        <Text style={{marginBottom: 10}}>Remove a view in React Native</Text>
        <SwitchIOS
          onValueChange={(value) => this.setState({showMap: value})}
          style={{marginBottom: 10}}
          value={this.state.showMap}
          />
        {map}
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: 50,
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  map: {
    height: 200,
    width: 300,
    margin: 10,
    borderWidth: 1,
    borderColor: '#000000',
  },

});

AppRegistry.registerComponent('TestMap', () => TestMap);

The relevant part being:

  render: function() {
    var map = this.state.showMap ? <MapView style={styles.map}/> : null;
    return (
      <View style={styles.container}>
        [..]
        {map}
      </View>
    );
  }
like image 106
peter Avatar answered Sep 18 '22 12:09

peter