Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a component is mounted in React-Native ES6

Tags:

react-native

I am setting a listener in my application and using force update whenever it is broadcasted but it gives error forceUpdate cant be called on unmounted component. How can I check if a component is mounted now that the isMounted() function is deprecated.

'use strict'; var React = require('react-native'); import ExpAndroid from './ExpAndroid'; var {   AppRegistry,   Image,   ListView,   TouchableHighlight,   StyleSheet,   Text,   View,   Component,   AsyncStorage,   Navigator,   DeviceEventEmitter } = React;  var rowID; var img=require('./resource/ic_pause_white.png');      class Example1 extends Component{   constructor(props) {     super(props);     this.state = {      };   }   componentWillMount(){        rowID = this.props.rowIdentity;       console.log("rowID "+rowID);    }   componentDidMount(){   console.log('component  mounted')   this.start();    DeviceEventEmitter.addListener('playMusicStatus', (data)=> {      if(data.playMusic==true){       img=require('./resource/ic_pause_white.png');        rowID++;         this.setState(this.state);         ExpAndroid.someMethod1("someurl);     }    }); }  componentWillUnmount(){   console.log('componentwill  unmounted') }    start() {      var  url = "some url";     ToastAndroid.prepareToPlay(url,true); }    render() {          return (       <Image source={require('./resource/album_back.png')} style={styles.background}>       <Image       source={{uri:this.state.trackDetails[rowID].thumnail_loc}}       style={styles.thumbnail}       />       <View style={styles.flowRow}>       <Text       style={styles.titles}       >text1 + {rowID}: </Text>       <Text       style={styles.titles}       >{this.state.details[rowID].text1}</Text>       </View>       <View style={styles.flowRow}>       <Text       style={styles.titles}       >text2 : </Text>       <Text       style={styles.titles}       >{this.state.details[rowID].text2}</Text>       </View>       <View style={styles.flowRow}>       <Text       style={styles.titles}       >Text3 : </Text>       <Text       style={styles.titles}       >{this.state.Details[rowID].Text3}</Text>       </View>       <View style={styles.flowRow}>       <Text       style={styles.titles}       >Text4 : </Text>       <Text       style={styles.titles}       >{this.state.details[rowID].Text4}</Text>       </View>         </Image>     );   } } var styles = StyleSheet.create({    container: {     flex: 1,    },   background: {     flex: 1,      width: null,     height: null,   },    flowRow : {     flexDirection :'row',    },   flowRowPlay : {     flexDirection :'row',     alignSelf:'center',    },   backgroundImage: {     flex: 1,     resizeMode: 'cover', // or 'stretch'   },    thumbnail: {     width: 100,     height: 120,     alignSelf:'center',     margin :30   },    controls: {     width: 30,     height: 30,     margin:20   },     titles: {     fontSize: 15,     margin:20,     color: 'white',    },   timings: {     fontSize: 12,     margin:5,     color: 'white',    }, });  module.exports = Example1; 
like image 490
stack Learner Avatar asked May 30 '16 09:05

stack Learner


People also ask

How do you check if a component is mounted React?

The useEffect() hook is called when the component is mounted and sets the mounted. current value to true . The return function from the useEffect() hook is called when the component is unmounted and sets the mounted. current value to false .

Is there a way to check if the React component is unmounted?

To check if the React component is unmounted, we can set a state in the callback that's returned in the useEffect hook callback. We create the isMounted state with the useState hook. Then we call the useEffect hook with a callback that calls setIsMounted to set isMounted to true .

How do I mount a component in React native?

var Home = React. createClass({ getInitialState: function() { return { componentSelected: 'One', userName: "Loading...", friendFeed: 'Loading...', loaded: false, loadedlocal: false, }; }, componentWillMount: function() { Method.

What is a mounted component React?

"Mounting" is when React "renders" the component for the first time and actually builds the initial DOM from those instructions.


1 Answers

You can handle this yourself in your component:

componentDidMount() {    this._mounted = true; }  componentWillUnmount() {   this._mounted = false; } 

Then you can check the value of this._mounted in your listener.

Please note that using forceUpdate() should be avoided https://facebook.github.io/react/docs/component-api.html#forceupdate

Normally you should try to avoid all uses of forceUpdate() and only read from this.props and this.state in render(). This makes your component "pure" and your application much simpler and more efficient.

like image 91
Jean Regisser Avatar answered Sep 19 '22 02:09

Jean Regisser