reactNativeReactNative系列React-Native

ReactNative组件间的通信

2017-02-22  本文已影响1233人  45b645c5912e

父组件向子组件通信

子组件向父组件通信

非父子组件之间的传值

demo示例

父子组件通信demo
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity
} from 'react-native';

export default class JSXDemo extends Component {
  render() {
    return (
      <View style={styles.bg}>
        <Parents />
      </View>

    );
  }
}


class Parents extends Component{
  render(){

    return(
      <View style={styles.parents}>
        <Childern ref='childern' name='我是父组件向子组件传递的参数' test={this.onParentClick1} />
        <Text >
          我是父组件
        </Text>
        <TouchableOpacity onPress={this.onParentClick2}>
          <Text style={styles.btn}>
            我是父组件按钮
          </Text>
        </TouchableOpacity>


      </View>
    )
  }
  onParentClick1 = ()=>{

    console.log('我是从父组件传递到子组件的方法');

  }
  onParentClick2 = ()=>{

    console.log(this.refs.childern.state.name);
    this.refs.childern.onChildenCilck2();

  }
}

class Childern extends Component {


  constructor(props) {
    super(props);
    this.state = {
      name : '我是子组件向父组件传递的参数'
    };
  }
  onChildenCilck1 = () =>{
    console.log(this.props.name);
    this.props.test();
  }

  onChildenCilck2 = () =>{

    console.log('我是子组件向父组件传递的方法');
  }

  render(){
    return(
      <View style={styles.childern}>
        <Text> 我是子组件 </Text>
          <TouchableOpacity onPress={this.onChildenCilck1}>
            <Text style={styles.btn}>
              我是子组件按钮
            </Text>
          </TouchableOpacity>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  bg:{
    flex:1,
    justifyContent:'center',
    alignItems:'center'
  },
  parents: {
    width:300,
    height:300,
    backgroundColor:'red',
    justifyContent:'center',
    alignItems:'center'
  },
  childern: {
    width:100,
    height:100,
    marginTop:10,
    backgroundColor:'green',
    justifyContent:'center',
    alignItems:'center'

  },
  btn:{

    backgroundColor:'yellow',

  }

});

AppRegistry.registerComponent('JSXDemo', () => JSXDemo);

Navigator传值

render() {
    let rootViewName = 'FirstView';
    let rootComponent = FirstView;

    return (
      <Navigator
        initialRoute = {{ name: rootViewName, component: rootComponent }}
        configureScene = {(route) => {
          return Navigator.SceneConfigs.HorizontalSwipeJump ;
        }}
        renderScene = {(route, navigator) => {
          let Component = route.component;
          return <Component {...route.params} navigator = {navigator} />
        }} />
    );
  }
push = (Id) =>{
    var _this = this;
    const navigator = this.props.navigator;
    if (navigator) {
      navigator.push({
        name: 'SecondView',
        component: SecondView,
        params: {
          id: Id,
          getUser: function(user) {
            _this.setState({
              user: user
            })
          }
        }
      });
    }
  }
componentDidMount() {
      this.setState({
        Id : this.props.id
      });
  }
push = (Id) =>{
    var _this = this;
    const navigator = this.props.navigator;
    if (navigator) {
      navigator.push({
        name: 'SecondView',
        component: SecondView,
        params: {
          id: Id,
          getUser: function(user) {
            _this.setState({
              user: user
            })
          }
        }
      });
    }
  }
pop = () =>{
    if (this.props.navigator) {
      this.props.navigator.pop();
      let user = USER_MODELS[this.state.Id];
      this.props.getUser(user);
    }
  }
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Navigator
} from 'react-native';
import FirstView from './FirstView'
export default class ZZHNavigator extends Component {
  render() {
    let rootViewName = 'FirstView';
    let rootComponent = FirstView;
    return (
      <Navigator
        initialRoute = {{ name: rootViewName, component: rootComponent }}
        configureScene = {(route) => {
          return Navigator.SceneConfigs.HorizontalSwipeJump ;
        }}
        renderScene = {(route, navigator) => {
          let Component = route.component;
          return <Component {...route.params} navigator = {navigator} />
        }} />
    );
  }
}
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,
  },
});
AppRegistry.registerComponent('ZZHNavigator', () => ZZHNavigator);
import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View
} from 'react-native';
import SecondView from './SecondView';
class FirstView extends Component {
  constructor(props) {
    super(props);
    this.state = {
      user : null
    };
  }
  push = (Id) =>{
    var _this = this;
    const navigator = this.props.navigator;
    if (navigator) {
      navigator.push({
        name: 'SecondView',
        component: SecondView,
        params: {
          id: Id,
          getUser: function(user) {
            _this.setState({
              user: user
            })
          }
        }
      });
    }
  }
  render() {
    return (
      <View style={styles.flex}>
        <Text style = {styles.btn} onPress = {() => this.push(0)}>
          查询ID为0的学生信息
        </Text>
        <Text style = {styles.btn} onPress = {() => this.push(1)}>
          查询ID为1的学生信息
        </Text>
        <Text style = {styles.btn} >
          {
            this.state.user? JSON.stringify(this.state.user) : ''
          }
        </Text>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  flex:{
    flex:1,
    justifyContent:'center',
    alignItems:'center',
  },
  btn:{
    marginTop:10,
    padding:5,
    backgroundColor:'red',
    color:'white',
  }
});
export default FirstView;
import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View
} from 'react-native';
const USER_MODELS = {
  0: { 姓名: '小明', 性别: '男' },
  1: { 姓名: '韩梅梅', 性别: '女' }
};
class SecondView extends Component {
  constructor(props) {
    super(props);
    this.state = {
      Id : null
    };
  }
  componentDidMount() {
      this.setState({
        Id : this.props.id
      });
  }
  pop = () =>{
    if (this.props.navigator) {
      this.props.navigator.pop();
      let user = USER_MODELS[this.state.Id];
      this.props.getUser(user);
    }
  }
  render() {
    return (
      <View style={styles.flex}>
        <Text style = {styles.btn}>
          我是学生{this.state.Id}信息
        </Text>
        <Text style = {styles.btn}>
          {
            JSON.stringify(USER_MODELS[this.state.Id])
          }
        </Text>
        <Text style = {styles.btn} onPress = {() => this.pop()}>
          点我返回上一页
        </Text>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  flex:{
    flex:1,
    justifyContent:'center',
    alignItems:'center',
  },
  btn:{
    marginTop:10,
    padding:5,
    backgroundColor:'red',
    color:'white',
  }
});
export default SecondView;
上一篇 下一篇

猜你喜欢

热点阅读