http://blog.csdn.net/p106786860/article/details/52408875
http://blog.csdn.net/p106786860/article/details/52408875
http://blog.csdn.net/p106786860/article/details/52408875
2016-09-02 01:21
1417人阅读
收藏
举报
分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
在编写React程序的时候,大家会遇到很多React组件之间的通信问题,主要分为以下3种:
1.父组件向子组件传值;
2.子组件向父组件传值;
3.没有任何嵌套的组件之间的传值(如兄弟组件之间传值)-后续章节介绍;
一、父组件向子组件传值
1.在父组件中通过属性传递给子组件,在子组件中通过this.props获取信息;
- 'use strict';
- import React from 'react';
- import {
- ... ...
- } from 'react-native';
-
- var MyAwesomeApp = React.createClass({
- getInitialState: function(){
- return{
- checked: false
- };
- },
- render: function(){
- return (
- <View style={styles.pagecontainer}>
-
- <ChildCompontent text='Toggle me' checked={this.state.checked}/>
- </View>
- )
- }
- });
-
-
-
- var ChildCompontent = React.createClass({
- render: function() {
- return (
-
- <View style={styles.childcompontent}>
- <Text>{this.props.text}</Text>
- <Switch value={this.props.checked}></Switch>
- </View>
- )
- }
- });
- var styles = StyleSheet.create({
- ... ...
- });
- AppRegistry.registerComponent('MyAwesomeApp', () => MyAwesomeApp);
运行如下如:
2.如果嵌套的层次太深,那么从外往内组件传递会比较纠结,需要通过props一层一层往下传递(当然你可以精简组件的层次);
- 'use strict';
- import React from 'react';
- import {
- ... ...
- } from 'react-native';
-
- var MyAwesomeApp = React.createClass({
- render: function(){
- return (
- <View style={styles.pagecontainer}>
- <ChildCompontent1 text='Toggle me too'/>
- </View>
- )
- }
- });
-
- var ChildCompontent1 = React.createClass({
- render: function() {
- return (
- <ChildCompontent2 text={this.props.text}/>
- )
- }
- });
- ... ...
-
- var ChildCompontentn = React.createClass({
- render: function() {
- return (
- <View style={styles.childcompontent}>
- <Text>{this.props.text}</Text>
- </View>
- )
- }
- });
- var styles = StyleSheet.create({
- pagecontainer: {
- flex: 1,
- flexDirection:'column',
- },
- ... ...
- });
- AppRegistry.registerComponent('MyAwesomeApp', () => MyAwesomeApp);
运行如下:
二、子组件向父组件传值
1.子组件控制自己的state,然后通过父组件提供的回调方法,告诉父组件信息并在组件展示出来;
2.其实是依赖于props来传递事件的引用,并通过回调的方式来实现;
3.如果嵌套太深的话,就必须一次次回调传入的回调函数,来实现子组件向父组件传值或者操作;
- 'use strict';
- import React from 'react';
- import {
- ... ...
- View,
- } from 'react-native';
-
- var MyAwesomeApp = React.createClass({
- getInitialState: function () {
- return {
- checked: false
- };
- },
-
- onChildChanged: function (newState) {
- this.setState({
- checked: newState
- });
- },
- render: function(){
- var isChecked = this.state.checked ? 'yes' : 'no';
-
-
- return (
- <View style={styles.pagecontainer}>
- <Text>Are you checked:{isChecked}</Text>
-
- <ChildCompontent text='Toggle me'
- initialChecked={this.state.checked}
- callbackParent={this.onChildChanged}/>
- </View>
- )
- }
- });
-
- var ChildCompontent = React.createClass({
- getInitialState: function () {
- return {
-
- checked: this.props.initialChecked
- };
- },
- render: function() {
- return (
- <View style={styles.childcompontent}>
- <Text>{this.props.text}</Text>
- <Switch value={this.state.checked} onValueChange={(value)=>{
-
- this.setState({checked: value});
- this.props.callbackParent(this.state.checked);
- }}/>
- </View>
- )
- }
- });
- var styles = StyleSheet.create({
- ... ...
- });
- AppRegistry.registerComponent('MyAwesomeApp', () => MyAwesomeApp);
运行如下:
4.多个子组件使用同一个回调的情况,当子组件改变的时候,使用同一个子组件的回到来给父组件返回值;
- 'use strict';
- import React from 'react';
- import {
- AppRegistry,
- ... ...
- } from 'react-native';
-
- var MyAwesomeApp = React.createClass({
- getInitialState: function () {
- return {
- initialChecked: false,
- totalChecked: 0
- };
- },
-
- onChildChanged: function (newState) {
- var newTotal = this.state.totalChecked + (newState ? 1 : -1);
- this.setState({
- totalChecked: newTotal,
- });
- },
- render: function(){
- var isChecked = this.state.checked ? 'yes' : 'no';
-
- return (
- <View style={styles.pagecontainer}>
- <Text>How many are checked:{this.state.totalChecked}</Text>
-
- <ChildCompontent text='Toggle me'
- initialChecked={this.state.initialChecked}
- callbackParent={this.onChildChanged}/>
- <ChildCompontent text='Toggle me too'
- initialChecked={this.state.initialChecked}
- callbackParent={this.onChildChanged}/>
- <ChildCompontent text='Toggle me too too '
- initialChecked={this.state.initialChecked}
- callbackParent={this.onChildChanged}/>
- </View>
- )
- }
- });
-
- var ChildCompontent = React.createClass({
- getInitialState: function () {
- return {
- checked: this.props.initialChecked
- };
- },
- render: function() {
- return (
- <View style={styles.childcompontent}>
- <Text>{this.props.text}</Text>
- <Switch value={this.state.checked} onValueChange={(value)=>{
- this.setState({checked: value});
- this.props.callbackParent(this.state.checked);
- }}/>
- </View>
- )
- }
- });
- var styles = StyleSheet.create({
- ... ...
- });
- AppRegistry.registerComponent('MyAwesomeApp', () => MyAwesomeApp);
运行如下: