Child to parent communication in React (JSX) without flux

前端 未结 3 1084
悲哀的现实
悲哀的现实 2021-01-02 09:16

I\'m really new to React, and I\'m pulling my hair out trying to solve what seems to me to be a simple problem. Here\'s a picture of the component I\'ve built.

Color

3条回答
  •  难免孤独
    2021-01-02 09:51

    OK, this is pretty simple in React without using flux or redux, similar to passing down props from parent to child, here we can use callback function like this:

    var colorsArray = ["#ED5851", "#9CCC64", "#337AEC", "#ff7a45", "#7E58C2", "#FFEB3B", "#78909C", "#FFFFFF", "#213a4b"];
    
    
    var EditDrawer = React.createClass({
        updateColor: function(i) {
            alert("New Color: " + i);
        },
        render: function(){
            return (
                
    {colorsArray.map(function(object, i){ return ; }, this)}
    ); } }); var ColorButton = React.createClass({ updateColor: function() { this.props.updateColor(this.props.buttonColor); }, render: function(){ var buttonStyle = { backgroundColor: this.props.buttonColor, }; return (
    this.props.buttonColor
    ) } });

提交回复
热议问题