react中组件间传值

子组件Children传值(msg)给父组件Parent
父组件

import React  ,{useState}from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import styles from './Welcome.less';
import Child from './Child';

export default (): React.ReactNode => {
  const [msg,setMsg] = useState('父组件传值给子组件')
  const [childMsg,setChildMsg] = useState('')

  const getChildrenMsg = (msg:any) => {
    setChildMsg(msg)
  }
    return (
      <PageHeaderWrapper content=" 这个页面只有 admin 权限才能查看">
        <div className={styles.content}>
          <h2>{msg}</h2>
          <hr/>
          <Child getChildrenMsg = {getChildrenMsg.bind(this)}/>
          <h2>子组件的值:{childMsg}</h2>
        </div>
      </PageHeaderWrapper>
    )
}

子组件

import React, {useState} from 'react'
import { Button} from 'antd';

export default function Child(props:any){
    const [msg,setMsg] = useState('子组件给父组件传值') 
    console.log('props',props)

    const toParent = ()=>{
        props.getChildrenMsg(msg)
    }

    return (
        <div>
            <h2>{ msg }</h2>
            <Button onClick={toParent}>传值</Button>
        </div>
    )
    
}

在这里插入图片描述
同级组件之间传值
同一父组件下平级组件间传值 ,简单一句话 子组件先传给父组件,父组件再传给那个子组件

// 处理平级组件间传值 ,先传给父组件,父组件再传给另一个组件
class Child1 extends React.Component {
    constructor (props) {
      super(props);
 
    }
    handleClick() {
      this.props.changeChild2Color('yellow')
    }
 
    render() {
      return (
        <div>
            <h1>Child1:  {this.props.bgColor}</h1>
            <button onClick={(e) => this.handleClick(e)}>向Child2传值,改变其颜色</button>
        </div>
      );
    }
}
class Child2 extends React.Component {
    constructor (props) {
      super(props);
    }
 
    render() {
      return (
        <div style={{background:this.props.bgColor}}>
            <h1>Child2:  {this.props.bgColor}</h1>
        </div>
      );
    }
}
class Father extends React.Component {
    constructor (props) {
      super(props);
      this.state={
        child2BgColor: '#999'
      };
    }
    onChild2BgColorChange(color) {
      this.setState({
        child2BgColor: color
      })
    }
    render() {
      return (
        <div>
          {/* 平级组件间传值*/}
          <Child1 changeChild2Color={(color)=>{this.onChild2BgColorChange(color)}}></Child1>
          <Child2 bgColor={this.state.child2BgColor}></Child2>
        </div>
      );
    }
}
ReactDOM.render(
  <div>
    <Father></Father>
  </div>,
  document.getElementById('app')
);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React ,有多种方式可以实现组件的值传递。以下是一些常见的方式: 1. Props(属性):通过在父组件给子组件传递属性,子组件可以通过 props 对象来获取这些属性值。父组件的属性变化会触发子组件的重新渲染。 2. State(状态):每个组件都有自己的状态对象,可以通过 setState 方法来更新状态并触发重新渲染。父组件可以通过 props 将状态值传递给子组件,并通过回调函数来修改父组件的状态。 3. Context(上下文):Context 提供了一种在组件共享数据的方式,可以在父组件创建一个 Context 对象,并通过 Provider 组件将数据传递给子组件。子组件可以通过 Consumer 组件或 useContext 钩子来访问这些数据。 4. Redux 或其他状态管理库:使用 Redux 或其他状态管理库可以将应用的状态集管理,并通过提供的 API 来进行状态的读取和更新。这样不同组件可以共享和传递状态,而不需要通过 props 层层传递。 5. 发布订阅模式或事件总线:可以使用发布订阅模式或事件总线来实现组件的解耦和通信。一个组件可以发布一个事件,其他组件可以订阅该事件并执行相应的操作。 这些是常见的组件传值方式,选择哪种方式取决于具体的场景和需求。在实际开发,根据项目的规模和复杂度,选择合适的方式来进行组件的数据传递。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值