Color调色板 (React版)

博客介绍了Color调色板案例中兄弟组件间的数据传递方式,即先将数据传递给父组件,再由父组件传递给另一个子组件,涉及前端组件数据交互知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

案例:Color调色板

在这里插入图片描述
兄弟组件之间的数据传递方式:
现将数据传递给父组件,父组件再将数据传递给另一个子组件。
在这里插入图片描述

const {Component,Fragment} = React;
//子组件 ColorBoard
class ColorBoard extends Component{
    render(){
        const {color} = this.props;
        return(
            <Fragment>
                <div className="colorBoard" style={{backgroundColor:color}}></div>
            </Fragment>
        )
    }
}

//子组件 ColorSelector
class ColorSelector extends Component{
    constructor(props){
        super(props);
        this.state = {
            colorItem:[255,255,255]
        }
    }
    //当滑竿发生改变时
    rangeChange(event,index){
        const {onColorChange} = this.props;
        let colorItem = [...this.state.colorItem];
        colorItem[index] = parseInt(event.target.value);
        this.setState({
            colorItem
        })
        onColorChange(this.state.colorItem);
    }
    render(){
        return(
            <Fragment>
                {
                    this.state.colorItem.map((item,index)=>{
                        return (
                            <input type="range" min={0} max={255} value={item}  key={index}
                            onChange={()=>this.rangeChange(event,index)}/>
                        )
                    })
                }
            </Fragment>
        )
    }
}

//父组件Color
class Color extends Component{
    constructor(props){
        super(props);
        this.state = {
            color:'#FFFFFF'
        }
    }
    colorChange(myColor){
        let color = '#' + myColor.map(item=>{
            return item>15?item.toString(16):'0'+item.toString(16)
        }).join('').toUpperCase();
        this.setState({
            color
        })
    }
    render(){
        return (
            <Fragment>
                <div className="container">
                    <div className="row">
                        <div className="col-md-12">
                            <h1>React兄弟组件的数据传递 <small>Color调色板</small></h1><hr/>
                        </div>
                    </div>
                    <div className="row">
                        <div className="col=md=5">
                            <ColorBoard color={this.state.color}></ColorBoard>
                        </div>
                        <div className="col-md-5">
                            <div>颜色代码:{this.state.color}</div>
                            <ColorSelector onColorChange={this.colorChange.bind(this)}></ColorSelector>
                        </div>
                    </div>
                </div>
            </Fragment>
        )
    }
}

ReactDOM.render(
    <Color></Color>,
    document.getElementById('colorPalette')
)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值