如何检查组件是否已安装在React-Native ES6中

时间:2016-05-30 09:04:21

标签: react-native

我正在我的应用程序中设置一个侦听器,并在广播时使用强制更新,但是它给出了错误forceUpdate无法在未安装的组件上调用。如果已弃用isMounted()函数,如何检查是否已装入组件。

'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;

3 个答案:

答案 0 :(得分:53)

您可以在组件中自行处理:

componentDidMount() { 
  this._mounted = true;
}

componentWillUnmount() {
  this._mounted = false;
}

然后你可以检查听众中this._mounted的值。

请注意,应避免使用forceUpdate() https://facebook.github.io/react/docs/component-api.html#forceupdate

  

通常你应该尽量避免使用forceUpdate(),只能在render()中读取this.props和this.state。这使您的组件“纯粹”,您的应用程序更简单,更高效。

答案 1 :(得分:2)

使用ReactUpdateQueue,您可以避免管理自己的isMounted州。

const ReactUpdateQueue = require('ReactUpdateQueue');

// Pass the ref to your component.
if (ReactUpdateQueue.isMounted(view)) {
  // Your component is mounted!
}

答案 2 :(得分:-1)

我所做的是更改componentWillMount中的回调。

let asyncCallback;

componentDidMount(){
    asyncCallback = res=> this.setState({data: res});
    asyncTask(asyncCallback);
}

componentWillUnmount(){
    asyncCallback = ()=> console.log("AsyncCallback called but component has unmounted");
}