React.js - using props or children to compare

React.js - using props or children to compare
0.0 0


I made up my own project, a very simple activity for English Learners. The user selects the word that matches the displayed color.

I am having trouble getting the checkMatch() function to actually check if the user is correct by comparing this.state.backgroundColor to the word the user selected. I think I need to use props or children, but maybe I’m wrong.

I provided the code below. Here is the link to the full code, if that’s easier.

Here is a snippet of the relevant code:

//check if color and word match... not working. 
   //Trying to access the children or the id of the "color" div rendered below.
  //I have tried:, this.props.children, id, and
  if('red' === this.state.backgroundColor){
    alert("Correct! It is red.")
  }else if('orange' === this.state.backgroundColor){
    alert("Correct! It is orange.")
  }else if('yellow' === this.state.backgroundColor){
    alert("Correct! It is yellow.")
  }else if('green' === this.state.backgroundColor){
    alert("Correct! It is green.")
  }else if('blue' === this.state.backgroundColor){
    alert("Correct! It is blue.")
  }else if('purple' === this.state.backgroundColor){
    alert("Correct! It is purple.")
    alert("Try again!")
   console.log(this.props.children) //undefined
      //Color Square. User gets a new color when they click.
      className = "square"
      style={{backgroundColor: this.state.backgroundColor}}
      //onClick={this.handleClick}> //this one doesn't work. Not sure why.
      onClick={() => this.handleClick()}>Click for a New Color!</div>
        <div className = "color" id = 'red' onClick={() => this.checkMatch()}>red</div>
        <div className = "color" id = 'orange' onClick={() => this.checkMatch()}>orange</div>
        <div className = "color" id = 'yellow' onClick={() => this.checkMatch()}>yellow</div>
        <div className = "color" id = 'green' onClick={() => this.checkMatch()}>green</div>
        <div className = "color" id = 'blue' onClick={() => this.checkMatch()}>blue</div>
        <div className = "color" id = 'purple' onClick={() => this.checkMatch()}>purple</div>

Thanks, again!


Ok… I see the problem here. For every color you have the same function that checks for all the colors.

so if u click red button for yellow the checkMatch() function checks if it is red then checks if it is orange the checks if it is yellow which is true…so for every color this is gonna repeat. I think you get my point…
So to make each function for each button unique you can do something like this:

  if(color === this.state.backgroundColor){
    alert("Correct! It is red.")
    alert("Try again!")
   console.log(this.props.children) //undefined

Your divs will be like this :

<div className = "color" id = 'red' onClick={() => this.checkMatch('red')}>red</div>

<div className = "color" id = 'orange' onClick={() => this.checkMatch('orange')}>orange</div>

repeat this format with correct color argument for every div.


I think it would help if you made your match conditions into a loop over an array of values to match for.

let colorMatch = ["red", "yellow", "blue"...];
for( let i = 0; i < colorMatch.length; i++ ){
  if( colorMatch[i] === this.state.backgroundColor ){
    alert(`Correct! It is ${colorMatch[i]}.`);
  alert("Try again!");


Of course! I think I have been staring at this for too long. Thank you both for your help!!