React native 组件间通讯

一、父组件向子组件通讯
通讯是单向的,数据必须是由一方传到另一方。在React中,父组件可以通过向子组件通过传props的方式,向子组件通讯。
import React,{Component,PureComponent} from 'react’
import {Text,View} from 'react-native’
class Parent extends Component {
constructor(props) {
super(props)
this.state = {
msg:'I am your father!’
}
}
render() {
return <Son_1 msg={this.state.msg} />;
}
}
class Son_1 extends PureComponent {
render() {
return ( <View> <Text>{this.props.msg}</Text> </View> )
}
}

如果父组件和子组件之间不止一个层级,如Parent 与 Child_1_1这样的关系,可通过…运算符(Object剩余和展开属性),将父组件的信息,以更简洁的方式传递给更深级的子组件。通过这种方式,不用考虑性能问题,通过babel转义后的…运算符性能与原生的一致,且上级组件props与state的改变,会导致组件本身及其子组件的生命周期改变。
class Son_1 extends PureComponent {
render() {
return (
<View>
<Text>{this.props.msg}</Text>
<Son_1_1 {…this.props}/>
</View>
)
}
}
class Son_1_1 extends PureComponent {
render() {
return (
<Text>{this.props.msg}</Text>
)
}
}

二、子组件向父组件通讯
在上一个例子中,父组件可以通过传递props的方式,自顶而下向子组件进行通讯。而子组件向父组件通讯,同样也需要父组件向子组件传递props进行通讯,只是父组件传递的,是作用域为父组件本身的函数,子组件调用该函数,将子组件想要传递的信息,作为参数,传递到父组件的作用域中
import React,{Component,PureComponent} from ‘react'
import {Text,TouchableOpacity} from 'react-native’
class Parent extends Component {
constructor(props) {
super(props)
this.state = {}
}
// 父组件定义的方法
onClickSon = (msgFromSon) => {
console.log(msgFromSon)
}
render() {
return (
<Son onClickSon={this.onClickSon}/>
)
}
}
class Son_1 extends PureComponent {
render() {
return (
<TouchableOpacity onPress={()=> this.props.onClickSon('I am your son')}>
<Text>爸爸去哪</Text>
</TouchableOpacity>

}
}

观察者模式-DeviceEventEmitter
解决了兄弟组件通信的问题
在传统的前端解耦方面,观察者模式作为比较常见的一种设计模式,大量使用在各种框架类库的设计当中。即使我们在写React,在写JSX,我们核心的部分还是JavaScript。
观察者模式也叫 发布者-订阅者模式,发布者发布事件,订阅者监听事件并做出反应,对于兄弟组件的通信,我们一般使用DeviceEventEmitter解决。
import React,{Component,PureComponent} from ‘react’
import {View,DeviceEventEmitter} from ‘react-native’
Class Parent extends Component {
render() {
return (
<View>
<Son_1/>
<Son_2/>
</View>
)
}
}
class Son_1 extends PureComponent {
componentDidMount() {
setTimeOut(() => {
// 发布msg事件
DeviceEventEmitter.emit(’sendMsg’,{text:’Hello Brother'});
},1000);

}

}
class Son_2 extends PureComponent {
componentDidMount() {
this.listener = DeviceEventEmitter.addListener(’sendMsg’,function(param){
// user param do somthing
})
};
// 最后别忘了移除通知
componentWillUnmount() {
this.listener.remove();
}
}

Push 传值
firstComponent

this.props.navigation.push('HelpCenterDetail',{pushParam: 'http://www.jianshu.com/u/d5b531888b2b'});

secondComponent [this.props.navigation.getParam('pushParam')]
constructor(props) {
super(props)
this.state = {
url:props.navigation.getParam('pushParam')
}
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 40、React 什么是React?React 是一个用于构建用户界面的框架(采用的是MVC模式):集中处理VIE...
    萌妹撒阅读 1,059评论 0 1
  • 作为一个合格的开发者,不要只满足于编写了可以运行的代码。而要了解代码背后的工作原理;不要只满足于自己的程序...
    六个周阅读 8,529评论 1 33
  • 原教程内容详见精益 React 学习指南,这只是我在学习过程中的一些阅读笔记,个人觉得该教程讲解深入浅出,比目前大...
    leonaxiong阅读 2,860评论 1 18
  • 目前,react组件有三种写法,分别是es5的createClass写法,es6的class写法,以及statel...
    ZoomFunc阅读 1,855评论 0 1
  • 时光啊!以它的永恒战胜一切,不喜不悲。 它永远平静的守着一切有生命的东西,从出生、成长、壮大、衰老乃至死亡,看一个...
    小猫咪咪1998阅读 300评论 0 7