代码之家  ›  专栏  ›  技术社区  ›  Christian Lopez

获取元素的背景颜色onclick-in-react

  •  -1
  • Christian Lopez  · 技术社区  · 6 年前

    我目前正在将我用普通HTML、CSS和JavaScript制作的RGB颜色猜测游戏转换为React。

    当我点击课堂上六个div中的一个时 coloredSquare ,我希望它获取该方块的背景色,并将其与屏幕上显示的来自元素的RGB颜色进行比较 mainColor 身份证件。

    在Vanilla JS中,它很简单,你只要 this.style.backgroundColor 内部 eventListener 但由于某种原因,我无法理解。我觉得自己真的很傻,我可能想得太多了,其实很简单。

    代码如下:

    import React, {Component} from "react";
    
    class RGBGuesser extends Component {
        constructor(){
            super();
            this.state = {
                correctCount: 0,
                displayCorrect: 0,
                colors: "", 
                chosenResult: "",
                chosenCorrect: 0,
            }
        }
    
        componentDidMount = () => {
            this.startGame();
        }
    
        initialGameState = () => {
            this.setState({
                colors: this.displayRandom(6)
            })
        }
    
        restart = () => {
            this.initialGameState();
            this.setState({
                chosenResult: "",
                chosenCorrect: 0,
                displayCorrect: 0
            })
        }
    
        pickSquare = () => {
            let colorRan = Math.floor(Math.random() * this.state.colors.length);
            return this.state.colors[colorRan]
        }
    
        displayRandom = amountSquares => {
            const colorArr = [];
            for(let i = 0; i < amountSquares; i++){
                colorArr.push(this.chooseRandom());
            }
            return colorArr;
        }
    
        chooseRandom = () => {
            let rColor = Math.floor(Math.random() * 256);
            let gColor = Math.floor(Math.random() * 256);
            let bColor = Math.floor(Math.random() * 256);
            return `rgb(${rColor}, ${gColor}, ${bColor})`;
        }
    
        chooseSquare = () => {
          //where i would want to do the logic of clicking the square and comparing it with the rgb color displayed on screen
        }
    
        startGame = () => {
            this.initialGameState();
            this.restart();
        }
    
        render(){
            let correctColor = this.pickSquare();
            return(
                <div>
                    <h1 id="header">RGB Color Guesser</h1>
                    <h3 id="mainColor">{correctColor}</h3>
                    <h3 id="result"></h3>
                    <h3 id="showCorrect">Number Correct: <span id="correctCount">0</span></h3>
                    <button id="startOver" onClick={this.restart}>Start Over</button>
                    <div id="colorGrid">
                        <div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[0]}}></div>
                        <div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[1]}}></div>
                        <div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[2]}}></div>
                        <div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[3]}}></div>
                        <div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[4]}}></div>
                        <div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[5]}}></div>
                    </div>
                </div>
            )
        }
    }
    
    export default RGBGuesser;
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   CamelBlues    6 年前
        chooseSquare = (e) => {
          console.log(e.currentTarget.style.background)
        }
    

    我认为将事件传递到事件处理程序中, currentTarget \ target 是你所缺少的

    另外,不要忘记在构造函数中绑定事件处理程序! constructor() { // snip this.chooseSquare = this.chooseSquare.bind(this); }