fac-15 / hangman

to fill in
0 stars 1 forks source link

Instead of storing previous in localStorage - perhaps try using React instead #7

Open helenzhou6 opened 5 years ago

helenzhou6 commented 5 years ago

https://github.com/fac-15/hangman/blob/cfb092c5140917637031a48952d3f61736073a3d/form.js#L51

jcharnley commented 5 years ago

in progress

helenzhou6 commented 5 years ago

Figured out ✨

Previously when trying to use prevState it didn't work since I didn't realise the state.displayArray would not always update (only updates if the letter exists in the hidden word - as part of the check() function) So instead this works:

checkExist = () => {
    this.setState({
      displayArray: check(
      this.state.randomWordGenerator,
      this.state.letterInput,
      this.state.displayArray)
    });

    this.setState({ letterInput: "" });

    if (!this.state.displayArray.includes(this.state.letterInput)) {
      this.setState({
        lives: this.state.lives - 1
      });
    }
  };

Where the lives - 1 when the new displayArray (after it's updated) does not include the letterInput