react native开发中,为了封装性经常需要自定义组件,这样就会出现父组件和子组件,那么怎么在父组件和子组件之间相互通信呢,也就是怎么在各自界面push和pop.传值.

父组件传递给子组件:

父组件:

在主组件里面,使用通过写一个子组件的属性,直接把值或者navigator传给子组件即可.如下20行:

 /**
* Sample React Native App
* https://github.com/facebook/react-native
* 父组件传递给子组件
* 父组件把值或者navigator传给子组件,然后在子组件里面实现push和显示
*/ import React, { Component } from 'react';
import ChildOne from './ChildOne'
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native'; export default class HomeOne extends Component {
render() {
return (
20 <ChildOne navigatorPush = {this.props.navigator} passValue = '我是一个父组件传给子组件的值'/>
);
}
} const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

子组件:

子组件这边可以直接使用主组件写的属性push和pop,通过this.props.属性名使用传过来的值.如下24行.31行

 /**
* Sample React Native App
* https://github.com/facebook/react-native
* 父组件传递给子组件
*/ import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
navigator,
} from 'react-native';
import OneDetails from './OneDetails'
export default class ChildOne extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome} onPress={()=>this.pushOneDetails()}>
我是子组件ONE
</Text>
<Text>
24 {this.props.passValue}
</Text>
</View>
);
}
pushOneDetails = ()=>{
30
31 this.props.navigatorPush.push({
32 component:OneDetails
33 })
}
} const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

子组件传递给父组件:

子组件:

自组件通过定义一个属性直接把事件传递给主组件,这样就可以在点击子组件实现在主组件里面实现push和pop,如下22行.28行.通过static....把值传给主组件使用,如行17-19


 /**
* Sample React Native App
* https://github.com/facebook/react-native
* 子组件传递给父组件
*/ import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native'; export default class ChildTwo extends Component {
1 static defaultProps = {
18 two: '我是子组件传给主组件的值'
19 };
render() {
return (
<Text style={styles.welcome} onPress={()=>this.passMenthod()}>
我是子组件TWO
</Text>
);
}
passMenthod = () =>{
28 this.props.pushDetails()
29 }
} const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

父组件:

      

父组件这边直接通过子组件的属性来接受事件,从而在主组件这边push和pop.如行29,行37-39.通过子组件.属性名.值使用子组件传过来的值,如行31

 /**
* Sample React Native App
* https://github.com/facebook/react-native
* 子组件传递给父组件
* 子组件把事件或值传递给父组件,然后在父组件push和显示
*/ import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
import ChildTwo from './ChildTwo'
import TwoDetails from './TwoDetails'
export default class HomeTwo extends Component {
// 构造
constructor(props) {
super(props);
// 初始状态
this.state = {
value:''
};
}
render() {
return (
<View style={styles.container}>
<ChildTwo pushDetails = {()=>this.pushDetails()} />
<Text>
31 {ChildTwo.defaultProps.two}
</Text>
</View>
);
}
pushDetails = ()=>{
37 this.props.navigator.push({
38 component:TwoDetails
39 })
}
} const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

项目代码:https://github.com/pheromone/react-native-childComponent-component

react native 之子组件和父组件之间的通信的更多相关文章

  1. React子组件和父组件通信

    React子组件和父组件通信包括以下几个方面: 子组件获取父组件属性:props或者state 子组件调用父组件的方法 父组件获取子组件的属性:props或者state 父组件调用子组件的方法 我们从 ...

  2. React篇-子组件调用父组件方法,并传值

    react 中子组件调用父组件的方法,通过props: 父组件: isNote(data){} <div className="tabC01"> <FTab ta ...

  3. react typescript 子组件调用父组件

    //父组件 import * as React from 'react'import { Input } from 'antd'const Search = Input.Searchimport &q ...

  4. 九、React中的组件、父子组件、React props父组件给子组件传值、子组件给父组件传值、父组件中通过refs获取子组件属性和方法

    一.概述 React中的组件: 解决html 标签构建应用的不足. 使用组件的好处:把公共的功能单独抽离成一个文件作为一个组件,哪里里使用哪里引入. [父子组件]:组件的相互调用中,我们把调用者称为父 ...

  5. react子组件向父组件传值

    子组件向父组件传值,注意父组件传递函数的时候必须绑定this到当前父组件(handleEmail={this.handleEmail.bind(this)}),不然会报错 /***实现在输入框输入邮箱 ...

  6. vue 子组件调用父组件的方法

    vue中 父子组件的通信: 子组件通过 props: { //子组件中写的. childMsg: { //字段名 type: Array,//类型 default: [0,0,0] //这样可以指定默 ...

  7. React-Native子组件修改父组件的几种方式,兄弟组件状态修改(转载)

    子组件修改父组件的状态,在开发中非常常见,下面列举了几种方式.DeviceEventEmitter可以跨组件,跨页面进行数据传递,还有一些状态的修改.http://www.jianshu.com/p/ ...

  8. 基于React Native的Material Design风格的组件库 MRN

    基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://git ...

  9. vuejs2.0子组件改变父组件的数据

    在vue2.0之后的版本中,不允许子组件直接改变父组件的数据,在1.0的版本中可以这样操作的,但是往往项目需求需要改变父组件的数据,2.0也是可一个,区别是,当我们把父元素的数据给子组件时,需要传一个 ...

  10. vuejs子组件向父组件传递数据

    子组件通过$emit方法向父组件发送数据,子组件在父组件的模板中,通过自定义事件接收到数据,并通过自定义函数操作数据 <!DOCTYPE html> <html lang=" ...

随机推荐

  1. 原生js通过prottype写的一个简单拖拽

    <!DOCTYPE html> <head> <meta charset="utf-8"/> <title></title&g ...

  2. Windows 10系统更换Windows 7系统磁盘分区注意事项一

    新买的电脑预装系统是WIN10,考虑到兼容性问题,打算更换为WIN7,但在新机上不能直接装WIN7系统,需要在BIOS启动中做一点小改动. 原因分析:由于Windows 8采用的是UEFI引导和GPT ...

  3. js中正则表达式 ---- 现成

    1 . 校验密码强度 密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间. ^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ 2. 校验中 ...

  4. 在为ListView设置adapter时出错

    为listView设置adapter,代码如下: SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.m ...

  5. [转载]Python 元组、列表、字典、文件

    python的元组.列表.字典数据类型是很python(there python is a adjective)的数据结构.这些结构都是经过足够优化后的,所以如果使用好的话,在某些area会有很大的益 ...

  6. 深入理解iOS开发中的BitCode功能

    前言 做iOS开发的朋友们都知道,目前最新的Xcode7,新建项目默认就打开了bitcode设置.而且大部分开发者都被这个突如其来的bitcode功能给坑过导致项目编译失败,而这些因为bitcode而 ...

  7. 【转】valueof()用法

    valueOf()用来返回对象的原始类型的值. 语法 booleanObject.valueOf() 代码:<script> var a = new String("valueO ...

  8. excel导出

    查询的结果结 List list  = commissionMService.getMapper().meishengExport(paramMap); // 第一步,创建一个webbook,对应一个 ...

  9. React Native 一个组件styles BUG

    'use strict'; var React = require('react-native'); var { StyleSheet, PanResponder, View, Text } = Re ...

  10. truncate,delete,drop的异同点

    说明:本文摘自oracle技术用户讨论组 truncate,delete,drop的异同点  注意:这里说的delete是指不带where子句的delete语句    相同点:truncate和不带w ...