react学习之组件间通信

react学习之组件间通信

代码:github-learn-react

父组件与子组件通信

通过props传递

export interface IChildProps {
  parentTellMe: string;
}
export interface IChildState {

}
export class Child extends React.Component<IChildProps, IChildState> {
  // constructor(props: IChildProps, state: IChildState) {
  //   super(props, state);
  // }
  render() {
    return (
      <div>child works! parent tell me: {this.props.parentTellMe}</div>
    );
  }
}
export interface IParentToChildProps {

}
export interface IParentToChildState {

}
export class ParentToChild extends React.Component<IParentToChildProps, IParentToChildState> {
  // constructor(props: IParentToChildProps, state: IParentToChildState) {
  //   super(props, state);
  // }
  render() {
    return (
      <div>
        parent-to-child works!
        <Child parentTellMe="呵呵哒!"></Child>
      </div>
    );
  }
}

子组件与父组件通信

通过父组件传递的props中添加函数来调用实现父组件与子组件通信

export interface IChildToParentProps {

}
export interface IChildToParentState {
  count: number;
}
export class ChildToParent extends React.Component<IChildToParentProps, IChildToParentState> {
  // constructor(props: IChildToParentProps, state: IChildToParentState) {
  //   super(props, state);
  // }
  state = { count: 1};
  public childTellMeAdd() {
    this.setState((prevState) => {
      return { count: prevState.count + 1};
    });
  }
  render() {
    return (
      <div>
        child-to-parent works!count: {this.state.count}
        <Child callback={this.childTellMeAdd.bind(this)}></Child>
      </div>
    );
  }
}
import React from 'react';
import './child.scss';

export interface IChildProps {
  callback: ()=>void;
}
export interface IChildState {

}
export class Child extends React.Component<IChildProps, IChildState> {
  constructor(props: IChildProps, state: IChildState) {
    super(props, state);
  }
  render() {
    return (
      <div>
        child works!
        <button onClick={this.props.callback}>tell parent add</button>
      </div>
    );
  }
}

跨组件通信

在父*父组件定义跨组件的对象内容和跨组件对象的的类型,并且定义获取对象的方法

    import PropTypes from 'prop-types';
export interface ICrossGradeProps {
  
}
export interface ICrossGradeState {
  count: number;
  myname: string;
}
export class CrossGrade extends React.Component<ICrossGradeProps, ICrossGradeState> {
  // constructor(props: ICrossGradeProps, state: ICrossGradeState) {
  //   super(props, state);
  // }
  state = {count: 1, myname: 'CrossGrade'};
  static childContextTypes = { // 定义跨数据类型
    count: PropTypes.number,
    name: PropTypes.string
  }
  getChildContext() { // 定义获取方法
    return {
      count: this.state.count,
      name: this.state.myname
    }
  }
  render() {
    return (
      <div>
        cross-grade works!
        <Parent />
      </div>
    );
  }
}

获取跨组件传递的值

import React from 'react';
import './child.scss';
import PropTypes from 'prop-types';
export interface IChildProps {

}
export interface IChildState {

}
export class Child extends React.Component<IChildProps, IChildState> {
//   constructor(props: IChildProps, state: IChildState) {
//     super(props, state);
//   }
  static contextTypes = { // 定义获取值的数据类型
    count: PropTypes.number,
    name: PropTypes.string
  }
  render() {
    return (
      <div>child works! {this.context.count}{this.context.name}</div>
    );
  }
}

跨组件从获取值的组件一直往上层组件找,找到最近的一个数据类型相同的就返回值

兄弟组件间通信

npm install events --save
import { EventEmitter } from "events";

通过上级组件传递或者定义一种类似单例的方式来实现发布订阅即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值