美文网首页
react Native DeviceEventEmitter

react Native DeviceEventEmitter

作者: 麦子_FE | 来源:发表于2016-10-17 10:25 被阅读2055次

这种方式不用关系到是父子组件 非父子组件

其实对应的就是reactjs的全局事件  react native使用全局事件不需要引入其他的库  react native自带DeviceEventEmitter模块

用法:

import {

DeviceEventEmitter

} from 'react-native';

发送  在你业务逻辑的代码里面写上 DeviceEventEmitter.emit('showTab',{active:true});

接收  componentDidMount(){

this.listener = RCTDeviceEventEmitter.addListener('通知名称',(value)=>{

// 接受到通知后的处理

});

}

componentWillUnmount(){

// 移除 一定要写

this.listener.remove();

}

另外context使用上下文可以让子组件直接访问祖先的数据或函数,无需从祖先组件一层层地传递数据到子组件中。

让子组件拿到父亲组件的数据

varMyContainer = React.createClass({

getInitialState:function(){return{

curItem:'item1'}

},

childContextTypes: {

curItem: React.PropTypes.any,

changeItem: React.PropTypes.any

},

getChildContext:function(){return{

curItem:this.state.curItem,

changeItem:this.changeItem

}

},

changeItem:function(item){this.setState({

curItem: item

});

},

render:function(){return(

)

}

});

相关文章

网友评论

      本文标题:react Native DeviceEventEmitter

      本文链接:https://www.haomeiwen.com/subject/hebayttx.html