组件通信传递数据

组件通信
父传子
Parent.js文件里(父组件)
import React from "react";
import { Child } from './Child'
export class Parent extends React.Component {
  state = {
    Name: 'zy'
  }
  render() {
    return (
      <div>
        <div>我是父组件,我的名字:{this.state.Name}</div>
        <div>我继承了:{this.props.money}块</div>
        <Child Name={this.state.Name} />
      </div>
    )
  }
}
Child.js文件了 (子组件)
import React from "react";
export class Child extends React.Component {
  render() {
    return (
      <div>
        <hr />
        <div>我是子组件,我名字{this.props.Name}</div>
      </div>
    )
  }


子传父 
Parent.js文件里(父组件)
import React, { Component } from 'react'
import './Parent.css'
import { Child } from './Child'
export class Parent extends Component {
  state = {
    money: 100
  }

  giveMoney = (money) => {
    console.log(money);
    this.setState({
      money: money
    })
  }
  render() {
    return (
      // 父组件
      <div className="parent">
        <h1>父组件</h1>
        <Child money={this.state.money} giveMoney={this.giveMoney} />
      </div>
    )
  }
}
Child.js文件了 (子组件)
import React, { Component } from 'react'

export class Child extends Component {
  render() {
    return (
      <div className="child">
        <h1>{this.props.money}</h1>
        <button onClick={() => this.props.giveMoney(300)}>xx</button>
      </div>

    )
  }
}

export default Child

兄弟直接传递数据
Parent.js文件里(父组件)
import React, { Component } from 'react'
import './Parent.css'
import { Child1 } from './Child1'
import { Child2 } from './Child2'
export class Parent extends Component {
  state = {
    msg: '',
    mag1: ''
  }

  setMsg = (msg) => {
    console.log(msg);
    this.setState({
      msg: msg
    })
  }
  setMsg1 = (mag1) => {
    console.log(mag1);
    this.setState({
      mag1: mag1
    })
  }
  render() {
    return (
      // 父组件
      <div className="parent">
        <h1>父组件</h1>
        <Child1 msg={this.state.msg} setMsg1={this.setMsg1} />
        <Child2 setMsg={this.setMsg} mag1={this.state.mag1} />
      </div>
    )
  }
}
Child1.js文件了 (子组件1)
import React, { Component } from 'react'

export class Child1 extends Component {
  render() {
    return (
      <div className="child">
        <h1>兄弟1 {this.props.msg}</h1>
        <button onClick={() => this.props.setMsg1('这就去')}>xx</button>
      </div>

    )
  }
}
Child2.js文件了 (子组件2)
import React, { Component } from 'react'

export class Child2 extends Component {
  render() {
    return (
      <div className="child">
        <h1>兄弟2{this.props.mag1}</h1>
        <button onClick={() => this.props.setMsg('挨揍了')}>xx</button>
      </div>

    )
  }
}
跨组件传递
import React, { Component } from 'react';
import './App1.css';

// 1 创建 Context 跨组件传值的上下文
const ThemeContext = React.createContext();
const { Provider, Consumer } = React.createContext();

const Node = () => (
  <div className="node">
    Node
    <SubNode />
  </div>
);
const SubNode = () => (
  <div className="sub-node">
    SubNode
    <Child />
  </div>
);

const Child = () => (
  <div className="child">
    Child
    <hr />
    <Consumer>
      {(value) => <div style={{ color: value }}>Childxxx</div>}
    </Consumer>
  </div>
);

export class App1 extends Component {
  render() {
    return (
      <Provider value={'pink'}>
        <div className="app">
          <h1>我是App组件</h1>
          <Node />
        </div>
      </Provider>
    );
  }
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值