From 09a5afebb0a3add530e90e258505a4edba6bc759 Mon Sep 17 00:00:00 2001 From: Arizmendi Date: Tue, 20 Sep 2022 21:54:03 -0700 Subject: [PATCH] modern react --- src/GuessControl.js | 52 +++++++++------------- src/NumberGuessingGame.js | 90 ++++++++++++++++----------------------- 2 files changed, 55 insertions(+), 87 deletions(-) diff --git a/src/GuessControl.js b/src/GuessControl.js index 64108b5..b111c68 100644 --- a/src/GuessControl.js +++ b/src/GuessControl.js @@ -1,47 +1,33 @@ -import React, { Component } from "react"; +import React, { useState } from "react"; import Button from "./Button"; -class GuessControl extends Component { - constructor(props) { - super(props); +const GuessControl = ({ onGuess }) => { - this.state = { - currentGuess: "", - }; - - /** - * These lines are required to make the methods/functions declared on this - * class have the correct `this` object when they run. - */ - this.handleInputChange = this.handleInputChange.bind(this); - this.onSubmitGuess = this.onSubmitGuess.bind(this); - } - - handleInputChange(event) { - this.setState({ currentGuess: event.target.value }); + const [currentGuess, setCurrentGuess] = useState('') + + const handleInputChange = (event) => { + setCurrentGuess( event.target.value ); } - onSubmitGuess() { + const onSubmitGuess = () => { // Since the values from an HTML input are strings by default, // convert to a number for the returned guess value // by passing in the string to the Number function. // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number - this.props.onGuess(Number(this.state.currentGuess)); - this.setState({ currentGuess: "" }); + onGuess(Number(currentGuess)); + setCurrentGuess(" "); } - render() { - return ( -
- - -
- ); - } + return ( +
+ + +
+ ); } export default GuessControl; diff --git a/src/NumberGuessingGame.js b/src/NumberGuessingGame.js index 07c5a24..68aab7c 100644 --- a/src/NumberGuessingGame.js +++ b/src/NumberGuessingGame.js @@ -1,4 +1,4 @@ -import React, { Component } from "react"; +import React, { useState } from "react"; import GuessControl from "./GuessControl"; import GuessMessage from "./GuessMessage"; import GameOver from "./GameOver"; @@ -13,65 +13,47 @@ function getRandomNumber() { const MAX_ATTEMPTS = 5; -class NumberGuessingGame extends Component { - constructor(props) { - super(props); +const NumberGuessingGame = () => { + const [numberToGuess, setNumberToGuess] = useState(getRandomNumber()); + const [numberOfGuesses, setNumberOfGuesses] = useState(0); + const [latestGuess, setLatestGuess] = useState(null); - this.state = { - numberToGuess: getRandomNumber(), - numberOfGuesses: 0, - latestGuess: null, - }; - - /** - * These lines are required to make the methods/functions declared on this - * class have the correct `this` object when they run. - */ - this.handleGuess = this.handleGuess.bind(this); - this.handleReset = this.handleReset.bind(this); - } - - handleGuess(guess) { - this.setState({ - latestGuess: guess, - numberOfGuesses: this.state.numberOfGuesses + 1, - }); + const handleGuess = (guess) => { + setLatestGuess(Number(guess)); + setNumberOfGuesses(numberOfGuesses + 1); } - handleReset() { - this.setState({ - numberToGuess: getRandomNumber(), - numberOfGuesses: 0, - latestGuess: null, - }); + const handleReset = () => { + setNumberToGuess(getRandomNumber()); + setNumberOfGuesses(0); + setLatestGuess(null); } - render() { - const isCorrectGuess = this.state.latestGuess === this.state.numberToGuess; + const isCorrectGuess = latestGuess === numberToGuess; + const isGameOver = isCorrectGuess || numberOfGuesses === MAX_ATTEMPTS; + + + return ( +
+

I'm thinking of a number from 1 to 100.

+

+ Can you guess the number I am thinking of in {MAX_ATTEMPTS}{' '} + tries? +

+ + {isGameOver && ( + + )} + {!isGameOver && ( + + )} +
+ ); - const isGameOver = - isCorrectGuess || this.state.numberOfGuesses === MAX_ATTEMPTS; - - return ( -
-

I'm thinking of a number from 1 to 100.

-

- Can you guess the number I am thinking of in {MAX_ATTEMPTS} tries? -

- - {isGameOver && ( - - )} - {!isGameOver && ( - - )} -
- ); - } } export default NumberGuessingGame;