React 组件传值

1.父组件向子组件传值(通过props)

(1)、子组件child.js

import React from "react";

export default class Child extends React.Component {
    constructor(props) {
        super(props)
    }

    render() {
        return (
            <div style={{ color: "red" }}>
                <div>
                    {this.props.name}
                </div>
            </div>
        )
    }
}

(2)、父组件father.js

import React from "react"
import Child from "./child"

export default class Father extends React.Component {
    constructor(props) {
        super(props)    
    }

    render() {
        return (
            <div>
                <Child name="abc"></Child>
            </div>
        )
    }
}

2.子组件向父组件传值(回调函数)

(1)、子组件

import React from "react";

export default class Child extends React.Component {
    constructor(props) {
        super(props)
    }

    handleClick() {
        let color = this.props.bgColor == "green" ? "#999" : "green"
        this.props.changeBgColor(color)
    }

    render() {
        return (
            <div style={{ color: "red" }}>
                <div>
                    {/* 2. 子组件给父组件传值*/}
                    {/* 子组件接收父组件的背景颜色值 */}
                    <div>父组件的背景色{this.props.bgColor}</div>
                    {/* 子组件执行改变颜色的函数 */}
                    <button onClick={(e) => { this.handleClick(e) }}>改变父组件的背景色</button>
                </div>

            </div>
        )
    }
}

(2)、父组件

import React from "react"
import Child from "./child"

export default class Father extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            bgColor: "#999",
        }
    }

    bgChange(color) {
        this.setState({
            bgColor: color
        })
    }

    render() {
        return (
            <div style={{ background: this.state.bgColor }}>
                <Child bgColor={this.state.bgColor} changeBgColor={(color) => { this.bgChange(color) }}></Child>
                {/* 父组件给子组件传值方法 */}
            </div>
        )
    }
}

3.兄弟组件传值(兄弟组件1传值给父组件,父组件在通过props传值给兄弟组件2)

(1)、brother1.js

import React from "react"

export default class Brother1 extends React.Component {
    constructor(props) {
        super(props)
    }

    handleClick() {
        // 改变兄弟的背景
        let color = this.props.color == "green" ? "yellow" : "green"
        this.props.changeColor(color)
    }

    render() {
        return (
            <div>
                <div>brother1</div>
                <button onClick={(e) => { this.handleClick(e) }}>改变brother2的背景</button>
            </div>
        )
    }
}

(2)、brother2.js

import React from "react"

export default class Brother2 extends React.Component {
    constructor(props) {
        super(props)
    }

    render() {
        return (
            <div style={{ background: this.props.color }}>
                <div>
                    brother2的背景色{this.props.color}
                </div>
            </div>
        )
    }
}

(3)、父组件

import React from "react"
import Brother1 from "./brother1";
import Brother2 from "./brother2";

export default class Father extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            color: "yellow"
        }
    }

    colorChange(color) {
        this.setState({
            color: color
        })
    }

    render() {
        return (
            <div>
                <Brother1 color={this.state.color} changeColor={(color) => { this.colorChange(color) }}></Brother1>
                <Brother2 color={this.state.color}></Brother2>
            </div>
        )
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值