passing parent's state to child's prop and is seen as 'undefined'

Viewed 27

I'm trying to build the game Yahtzee.. and I'm facing an issue when trying to pass from parent class DiceRow the value of a randomly chosen number to child class Die, in function initDice within DiceRow:

import React, { Component } from 'react'
import Die from './Die';
import './DiceRow.css';
import { rollDie } from './helper';

export default class DiceRow extends Component {
    constructor(props){
        super(props);
        this.state = {
            dices: [rollDie(), rollDie(), rollDie(), rollDie(), rollDie()],
            isDisabled: [false, false, false, false, false]
        }
        this.handleRoll = this.handleRoll.bind(this);
        this.handleDieClick = this.handleDieClick.bind(this);
        this.initDice = this.initDice.bind(this);

    }

    handleDieClick(id){ // means we need to disable the clicked die

    }

    handleRoll(){
        let new_dices_values = [];

        for(let i=0; i<this.state.dices.length; i++){
            if(!this.state.isDisabled[i]){ // only if is disabled === false
                new_dices_values[i]=rollDie();
            }
            else {
                new_dices_values[i]=this.state.dices[i];
            }
        }
d
        this.setState({
            dices: new_dices_values
        });
        

    }

    initDice(){
        let dices = [];
        for(let i=0; i<this.state.dices.length; i++){
            dices[i] = <Die onClick={this.handleDieClick} value={this.state.dices[{i}]} key={i} id={i}/>
        }
        return dices;
    }

  render() {
    let dices = this.initDice();
    return (
        <div>
      <div className='DiceRow'>
            {dices}
        <div>
        </div>
      </div>
        <button onClick={this.handleRoll} className='DiceRow-RollBtn'>Roll Dice!</button>
      </div>
    )
  }
}

Die.js

import React, { Component } from 'react';
import './Die.css';
import {rollDie} from './helper';

export default class Die extends Component {

    constructor(props){
        super(props);
        this.state = {
            isRolling: false,
            disableRolling: false
        }
    }
  render() {

    let whichDie = <i className={`fa-solid fa-dice-${this.props.value} fa-5x`}></i>
    
    return (
      <div>
        <div className='Die'>
            {whichDie}
        </div>
    </div>
    )
  }
}

enter image description here

which results in my Dice not to appear ;'(

Thanks in advance, may you all have a day without compilation errors :-D

1 Answers

So, the problem was the following:

initDice(){
        let dices = [];
        for(let i=0; i<this.state.dices.length; i++){
            dices[i] = <Die onClick={this.handleDieClick} value={this.state.dices[{i}]} key={i} id={i}/>
        }
        return dices;
    }

let's observe the

value={this.state.dices[{i}]}

We can notice the JSX curly braces in order for a JavaScript Expression to be interpreted.

If we notice, my issue here was, as stated in the comments by @Abbas Shaikh, I have additionally added { } around the i variable. That results in the { } brackets, surrounding the i variable, a declaration of an object (JS syntax), and is an object which is not defined properly, hence, the received value is 'undefined'.

Related