0

Previously successful callback method to send data from child component to parent component not working in this new project.

I'm working on a React web app. There's a component named Flags.js. It contains US state flags, and a targetFlag state. The Flags component loads correctly, but doesn't send that targetFlag selection to App.js. I previously asked about child to parent communications and got this working answer for a different project: React communication problem from child to parent. I tried applying that logic to this app but no success.

Github: https://github.com/irene-rojas/us-flags

App.js

import React, { Component } from 'react';
import './App.css';
import SVGMap from "./components/Map/Map.js";
import Flags from "./components/Flags/Flags";


class App extends Component {

state = {
    correct: 0,
    wrong: 0,
    targetFlag: "",
    selectedState: ""
}

onClick = (event) => {
    event.preventDefault();
    let clickedState = event.target.id;
    this.setState({
        selectedState: clickedState
    });  
    if (clickedState === this.state.selectedState) {
        this.setState({correct: this.state.correct + 1}, () => {
            console.log("correct");
            this.getFlag();
        });
    };
    if (clickedState === !this.state.selectedState) {
        this.setState({wrong: this.state.wrong + 1}, () => {
            console.log("wrong");
            this.getFlag();
        });
    }
}

getFlag = (targetFlag) => {
    this.setState({
        targetFlag: targetFlag.id
    });
    console.log(`App.js: ${this.state.targetFlag}`)
}


  render() {
    return (
  <div className="App">

    <div className="header">
        <h1>Match the Flag</h1>
    </div>

    <div className="flagsDiv">
        <Flags 
            sendFlag={this.getFlag}
        />
    </div>

    <div className="mapDiv">
        <SVGMap 
            onClick={this.onClick}
        />
    </div>

    <div className="scoreDiv">
        Correct: {this.state.correct}
        <br></br>
        Wrong: {this.state.wrong}
        <br></br>
        Target State: {this.state.targetFlag}
        <br></br>
        Selected State: {this.state.selectedState}
    </div>

  </div>
    );
  }
}

export default App;

Flags.js

import React, { Component } from 'react';
import "./Flags.css";
import alabama from "./images/Alabama.png";
import alaska from "./images/Alaska.png";
--- 48 more states in this import format ---

class Flags extends Component {

state = {
    flags: [
        {name: "Alaska", src: alaska, id: "AK"},
        {name: "Hawaii", src: hawaii, id: "HI"},
--- 48 more states in this state format ---
    targetFlag: [
        {name: "", src: "", id: ""}
    ],
};

componentDidMount() {
    let targetFlag = this.state.flags[Math.floor(Math.random()*this.state.flags.length)];
        this.setState({
            name: targetFlag.name,
            src: targetFlag.src,
            id: targetFlag.id
        });
        // console.log(`Flags.js: ${targetFlag.name}`);
}

sendFlag = (targetFlag) => {
    this.props.sendFlag(targetFlag);
}  


render() {
    return (

    <div>
        <img  
            src={this.state.src} 
            alt={this.state.id}
            onLoad={this.sendFlag}
        />
        <br></br>
        {this.state.name}
    </div>

    )
    }
}

export default Flags;

I want the targetFlag value chosen in Flags.js to transfer to App.js. Please let me know if there are other problems with the code.

Irene Rojas
  • 57
  • 3
  • 9

1 Answers1

1

I pulled your github repo, You should notice that setstate is async, so you should use callback function

//App.js line 36
//notice the targetFlag.target.id and setstate callback function
getFlag = (targetFlag) => {
        this.setState({
            targetFlag: targetFlag.target.id
        }, ()=>{
            console.log(`App.js: ${this.state.targetFlag}`)
        });

    }

//Flags.js line 133
//notice the addition of id attribute
<img  
  src={this.state.src} 
  alt={this.state.id}
  onLoad={this.sendFlag}
  id={this.state.id}
 />
Alvin Theodora
  • 936
  • 1
  • 11
  • 18
  • Thank you, this worked! The targetState now appears. Though the game logic still isn't working. I moved this.getFlag() out of setState. onClick = (event) => { event.preventDefault(); let clickedState = event.target.id; this.setState({ selectedState: clickedState }); if (clickedState === this.state.selectedState) { this.setState({correct: this.state.correct + 1}, () => { console.log("correct"); }); this.getFlag(); }; } – Irene Rojas Jan 24 '19 at 03:10