我对 React 很陌生。我正在通过创建一个非常简单的九个网格框来练习,用户可以在其中使用下拉菜单选择他们目前想要使用的颜色。唯一的问题是,我无法弄清楚如何将变量从包含它的类(ColorPicker)传递到包含网格的类(Box)。谁能给我一些关于如何做到这一点的指示?
我仍然习惯于将 props 传递给其他类。
这是 CodePen 的链接:http ://codepen.io/anfperez/pen/RorKge
这是我的代码
//this displays the color selections for the boxes: red, green, and blue
var ColorPicker = React.createClass({
handleChange: function(e) {
    var newColor = e.target.value;
    this.props.onChange(color);
},
render: function() {
    
return (
    <div>
        <select id="pick-colors" onChange={this.handleChange}>
            <option value="red">
                Red 
            </option>
            <option value="green">
                Green 
            </option>
            <option value="blue">
                Blue 
            </option>
        </select>
    </div>
    )
}
});
//contains the boxes that will eventually change color
var Box = React.createClass({
getInitialState: function() {
    return {
      //boxes are initially white
        color: 'white'
    };
},
    changeColor: function(newColor) {
        var newColor = this.state.color;
        this.setState({
            color: newColor
        });
    },
render: function() {
    return (
    <div >
    <div className='box' style={{background:this.state.color}} onClick={this.changeColor}> 
    </div>
    </div>
    );
}
});