react native 之子组件和父组件之间的通信

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

父组件传递给子组件:

父组件:

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

 1 /**
 2  * Sample React Native App
 3  * https://github.com/facebook/react-native
 4  *  父组件传递给子组件
 5  *  父组件把值或者navigator传给子组件,然后在子组件里面实现push和显示
 6  */
 7
 8 import React, { Component } from ‘react‘;
 9 import ChildOne from ‘./ChildOne‘
10 import {
11     AppRegistry,
12     StyleSheet,
13     Text,
14     View
15 } from ‘react-native‘;
16
17 export default class HomeOne extends Component {
18     render() {
19         return (
20           <ChildOne navigatorPush = {this.props.navigator} passValue = ‘我是一个父组件传给子组件的值‘/>
21         );
22     }
23 }
24
25 const styles = StyleSheet.create({
26     container: {
27         flex: 1,
28         justifyContent: ‘center‘,
29         alignItems: ‘center‘,
30         backgroundColor: ‘#F5FCFF‘,
31     },
32     welcome: {
33         fontSize: 20,
34         textAlign: ‘center‘,
35         margin: 10,
36     },
37     instructions: {
38         textAlign: ‘center‘,
39         color: ‘#333333‘,
40         marginBottom: 5,
41     },
42 });

子组件:

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

 1 /**
 2  * Sample React Native App
 3  * https://github.com/facebook/react-native
 4  *  父组件传递给子组件
 5  */
 6
 7 import React, { Component } from ‘react‘;
 8 import {
 9     AppRegistry,
10     StyleSheet,
11     Text,
12     View,
13     navigator,
14 } from ‘react-native‘;
15 import OneDetails from ‘./OneDetails‘
16 export default class ChildOne extends Component {
17     render() {
18         return (
19             <View style={styles.container}>
20                 <Text style={styles.welcome}  onPress={()=>this.pushOneDetails()}>
21                     我是子组件ONE
22                 </Text>
23                 <Text>
24                     {this.props.passValue}
25                 </Text>
26             </View>
27         );
28     }
29     pushOneDetails = ()=>{
30
31         this.props.navigatorPush.push({
32         component:OneDetails
33     })
34 }
35 }
36
37 const styles = StyleSheet.create({
38     container: {
39         flex: 1,
40         justifyContent: ‘center‘,
41         alignItems: ‘center‘,
42         backgroundColor: ‘#F5FCFF‘,
43     },
44     welcome: {
45         fontSize: 20,
46         textAlign: ‘center‘,
47         margin: 10,
48     },
49     instructions: {
50         textAlign: ‘center‘,
51         color: ‘#333333‘,
52         marginBottom: 5,
53     },
54 });

父组件传递给子组件:

子组件:

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



 1 /**
 2  * Sample React Native App
 3  * https://github.com/facebook/react-native
 4  *  子组件传递给父组件
 5  */
 6
 7 import React, { Component } from ‘react‘;
 8 import {
 9     AppRegistry,
10     StyleSheet,
11     Text,
12     View
13 } from ‘react-native‘;
14
15
16 export default class ChildTwo extends Component {
17     static defaultProps = {
18         two: ‘我是子组件传给主组件的值‘
19     };
20     render() {
21         return (
22                 <Text style={styles.welcome} onPress={()=>this.passMenthod()}>
23                     我是子组件TWO
24                 </Text>
25         );
26     }
27     passMenthod = () =>{
28         this.props.pushDetails()
29     }
30 }
31
32 const styles = StyleSheet.create({
33     container: {
34         flex: 1,
35         justifyContent: ‘center‘,
36         alignItems: ‘center‘,
37         backgroundColor: ‘#F5FCFF‘,
38     },
39     welcome: {
40         fontSize: 20,
41         textAlign: ‘center‘,
42         margin: 10,
43     },
44     instructions: {
45         textAlign: ‘center‘,
46         color: ‘#333333‘,
47         marginBottom: 5,
48     },
49 });

父组件:

      

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

 1 /**
 2  * Sample React Native App
 3  * https://github.com/facebook/react-native
 4  *  子组件传递给父组件
 5  *  子组件把事件或值传递给父组件,然后在父组件push和显示
 6  */
 7
 8 import React, { Component } from ‘react‘;
 9 import {
10     AppRegistry,
11     StyleSheet,
12     Text,
13     View
14 } from ‘react-native‘;
15 import ChildTwo from ‘./ChildTwo‘
16 import TwoDetails from ‘./TwoDetails‘
17 export default class HomeTwo extends Component {
18     // 构造
19       constructor(props) {
20         super(props);
21         // 初始状态
22         this.state = {
23             value:‘‘
24         };
25       }
26     render() {
27         return (
28             <View style={styles.container}>
29             <ChildTwo pushDetails = {()=>this.pushDetails()} />
30                 <Text>
31                     {ChildTwo.defaultProps.two}
32                 </Text>
33             </View>
34         );
35     }
36     pushDetails = ()=>{
37         this.props.navigator.push({
38             component:TwoDetails
39         })
40     }
41 }
42
43 const styles = StyleSheet.create({
44     container: {
45         flex: 1,
46         justifyContent: ‘center‘,
47         alignItems: ‘center‘,
48         backgroundColor: ‘#F5FCFF‘,
49     },
50     welcome: {
51         fontSize: 20,
52         textAlign: ‘center‘,
53         margin: 10,
54     },
55     instructions: {
56         textAlign: ‘center‘,
57         color: ‘#333333‘,
58         marginBottom: 5,
59     },
60 });

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

时间: 2024-12-22 10:26:03

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

React子组件和父组件通信

React子组件和父组件通信包括以下几个方面: 子组件获取父组件属性:props或者state 子组件调用父组件的方法 父组件获取子组件的属性:props或者state 父组件调用子组件的方法 我们从下面这个例子来详细了解: 1 var Father=React.createClass({ 2 getDefaultProps:function(){ 3 return { 4 name:"父组件" 5 } 6 }, 7 MakeMoney:function(){ // 挣钱,供子组件调用

React篇-子组件调用父组件方法

react 中子组件调用父组件的方法,通过props: 父组件: <div className="tabC01"> <FTab tabCon={'tabCon01'} note={()=>this.isNote()}/></div> 子组件: <span className="wh01" >股票持仓(前十)<img src={require("../../image/[email protecte

react ,父子调用子组件的方法与子组件调用父组件的方法

1.父组件调用子组件的方法给子组件的标签 定义一个属性,例如 ref="zizu" ------------- 父组件通过,this.refs.biaoji.dream('哈哈') //调用子组件的dream方法 2.子组件调用父组件的方法 2.1.首先父组件需要通过给子组件自定义属性,把方法传递给子组件.2.2.子组件通过this.props 接收父组件的方法,this.props.方法名称().这样就可以调用父组件的方法了 原文地址:https://www.cnblogs.com/

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

子组件通过$emit方法向父组件发送数据,子组件在父组件的模板中,通过自定义事件接收到数据,并通过自定义函数操作数据 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript" src="

组件之间的通信(子组件-改变父组件的值)

在vue中,组件之间的通信,是不建议子组件改变父组件的值,因为一个父组件有可能会有很多子组件,改来改去会很难管理(别人都这样说,我信) 试试: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src='../vue.js'> </script> </head> <bod

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

基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://github.com/binggg/mrn 示例应用在线演示 https://appetize.io/app/j48zj9r83cetpd1mhg4g8buc4w 示例应用下载 https://github.com/binggg/MaterialReactNative/blob/master/androi

vue系列(一)子组件和父组件

父组件传递数据到子组件props 父组件 <template> <div class="main"> <div class="top"> <span :class="{action:ind===index}" v-for="(item,index) in lanMenu" v-on:click="clickMenu(index,item.con)">{{ite

vue2.0 子组件和父组件之间的传值

Vue是一个轻量级的渐进式框架,对于它的一些特性和优点在此就不做赘述,本篇文章主要来探讨一下Vue子父组件通信的问题 首先我们先搭好开发环境,我们首先得装好git和npm这两个工具(如果有不清楚的同学请自行百度哦) 环境搭建步骤: 打开git ,运行 npm install --global vue-cli 这是安装vue的命令行 vue init webpack vue-demo 这是vue基于webpack的模板项目 cd vue-demo 进入vue-demo文件夹 npm install

vue2.0子组件修改父组件props数据的值

从vue1.0升级至2.0之后 prop的.sync被去除 因此直接在子组件修改父组件的值是会报错的如下: 目的是为了阻止子组件影响父组件的数据那么在vue2.0之后 如何在子组件修改父组件props传过来的值呢?思路是通过子组件$emit发射一个方法  如下: this.$emit('imgDataCallback', callbackUrl); 在父组件使用子组件的地方用v-on绑定这个自定义事件 如下: 然后在父组件定义这个方法 //获取imgurl        getImgData: