美文网首页
React-Native 组件封装与父子组件通信样例(初学)(二

React-Native 组件封装与父子组件通信样例(初学)(二

作者: Jason_兵哥 | 来源:发表于2018-06-25 15:44 被阅读504次

需求假设:封装自定义组件,可进行数字的自由增减,并在主页面进行求和联动

准备知识:

1. extends Component 继承组件类, 

2. constructor(props) 重写父类方法,可在里面进行变量定义

3. state与props: 用于存储数据

4. 父子通信: 父传子通过props传递, 子传父通过回调父页面方法实现

5. render: 渲染页面,类似于html,主要由react-native组件完成

6. StyleSheet.create 创建样式定义

7. 总结:Vue父子组件通信,父传子通过props, 子传父通过$emit原理应该都差不多

8. 源代码地址: https://github.com/ysb002003/ReactNativeLearning

9. 实现效果

开始代码:

1.目录结构:

2. 主界面代码 App.js

/**

* Sample React Native App

* https://github.com/facebook/react-native

* @flow

*/

import React, { Component } from 'react';

import {

  Platform,

  StyleSheet,

  Text,

  View

} from 'react-native';

import Counter from './src/component/Counter';

type Props = {};

export default class App extends Component {

  constructor(props) {

    super(props); //调用父类

    this.initValues = [1, 2, 3];

    const initSum = this.initValues.reduce((a, b) => a + b, 0);  //求和,0为默认值

    this.state = {

      totalNumber: initSum

    };

    this._updateNumber = this._updateNumber.bind(this); //在constructor中进行函数绑定,否则报错

  };

  _updateNumber(newValue, oldValue) {  //子组件会调用该函数进行数据通信

    const value = newValue - oldValue;

    this.setState({ totalNumber: this.state.totalNumber + value });

  };

}

const styles = StyleSheet.create({

  container: {

    flex: 1,

    justifyContent: 'center',

    alignItems: 'center',

    backgroundColor: '#F5FCFF',

    borderWidth: 10

  },

  welcome: {

    fontSize: 30,

    fontWeight: 'bold',

    textAlign: 'center',

    margin: 10,

    color: 'red'

  },

});

3. 子组件代码 Counter.js

import React, { Component } from 'react';

import {

    StyleSheet,

    View,

    Text,

    TouchableOpacity,

    TextInput

} from 'react-native';

import PropTypes from 'prop-types';

export default class Counter extends Component {

    constructor(props) { //构造函数

        super(props);

//使用state存储父页面传过来的参数,禁止直接修改主页面传来的props

this.state = {

            value: this.props.initValue || 1,  

            style: this.props.style,

        };

    }

    static defaultProps = {

        initValue: 1,

        onUpdateNumber: f => f  // 子调父的函数中介

    }

    _checkNumber() {

        let number = this.state.value;

        if (number == '' || number < 1) {

            number = 1;

        } else {

            number = Math.floor(number); //取最接近的int,固定写法

        }

        this.setState({ value: number });

    }

    _reduce() {

        this._changeText(this.state.value - 1);

    }

    _plus() {

        this._changeText(this.state.value + 1);

    }

    _changeText(txt) {

        this.setState({ value: Number(txt) });

        this.props.onUpdateNumber(txt, this.state.value); //调用父组件函数,进行数据通信

    }

};

const styles = StyleSheet.create({

    operatingBox: {

        width: 120,

        height: 35,

        borderColor: '#ddd',

        borderWidth: 1,

        flexDirection: 'row',

        alignItems: 'center',

        borderRadius: 5,

        overflow: 'hidden'

    },

    reduceBox: {

        width: 34,

        height: 34,

        alignItems: 'center',

        justifyContent: 'center',

        borderRightWidth: 1,

        borderRightColor: '#ddd'

    },

    btnReduce: {

        fontSize: 18,

        textAlign: 'center',

        backgroundColor: 'transparent'

    },

    inputBox: {

        flex: 1,

        borderRightWidth: 1,

        borderRightColor: '#ddd'

    },

    input: {

        flex: 1,

        padding: 0,

        textAlign: 'center',

        backgroundColor: 'transparent',

        fontSize: 14

    },

    plusBox: {

        width: 34,

        height: 34,

        alignItems: 'center',

        justifyContent: 'center'

    },

    btnPlus: {

        fontSize: 18,

        textAlign: 'center',

        backgroundColor: 'transparent'

    },

});

Counter.propTypes = {

    initValue: PropTypes.number,

    style: PropTypes.object

};

相关文章

  • React-Native 组件封装与父子组件通信样例(初学)(二

    需求假设:封装自定义组件,可进行数字的自由增减,并在主页面进行求和联动 准备知识: 1. extends Comp...

  • vue中的组件通信

    一、组件通信(组件传值) 1.1父子组件通信 1.2子父组件通信 1.3非父子组件通信(兄弟组件通信)

  • 2018-05-23 (表严肃)笔记二

    二、组件 全局及局部组件 配置组件点赞的实现: 组件通信之父子通信(1) 父子通信 props: 在当前页面中点击...

  • 组件通信

    组件关系 组件关系可以分为父子组件通信、兄弟组件通信、跨级组件通信。 父子组件通信 1. 子组件使用 $emit(...

  • 生命周期,组件通信、插槽

    一、生命周期 二、组件通信 Vue只有两种关系, 父子组件 非父子组件 1.父子通信:当我们需要把父组件中的数据传...

  • vue组件通信,中央事件总线

    vue 组件通信分为父组件与子组件通信、子组件与父组件通信、非父子关系组件通信三种 第一种大家都知道用props,...

  • Vue入门系列(五)组件通信

    组件内通信主要分为两种:父子组件通信和子组件之间通信。 1.父子组件通信 父组件通过props属性向子组件传递数据...

  • Vue如何实现组件通信?

    Vue组件通信的三种情况: 父子通信 爷孙通信 兄弟通信 父子通信:父组件使用Prop向子组件传递数据,子组件通过...

  • Vue事件总线(EventBus)

    vue组件非常常见的有父子组件通信,兄弟组件通信。而父子组件通信就很简单,父组件会通过props向下传数据给子组件...

  • [Vue]组件之间如何通信?

    组件通信可以大致分为两种情况,父子组件之间的通信和非父子组件之间的通信,下面来分别介绍 一、父子组件之间的通信 1...

网友评论

      本文标题:React-Native 组件封装与父子组件通信样例(初学)(二

      本文链接:https://www.haomeiwen.com/subject/kttkyftx.html