Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson_2_1 React Router Class Components #35

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const buttonStyle = {
minHeight: "2em",
};

// eslint-disable-next-line react/prop-types
export default function Button({ onClick, children }) {
return (
<button style={buttonStyle} onClick={onClick}>
Expand Down
2 changes: 2 additions & 0 deletions src/GameOver.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Button from "./Button";

// eslint-disable-next-line react/prop-types
function GameOver({ hasWon, onReset }) {
return (
<div>
{hasWon && <h2>Congratulation! You guessed my number.</h2>}
{!hasWon && (
// eslint-disable-next-line react/no-unescaped-entities
<h2>You didn't guess my number. Would you like to try again?</h2>
)}
<Button onClick={onReset}>Play Again!</Button>
Expand Down
100 changes: 63 additions & 37 deletions src/GuessControl.jsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,73 @@
import React, { Component } from "react";
import { useState } from "react";
import Button from "./Button";
import PropTypes from "prop-types";

class GuessControl extends Component {
constructor(props) {
super(props);
function GuessControl(props) {
const [currentGuess, setCurrentGuess] = useState("");

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 });
function handleInputChange(event) {
setCurrentGuess(event.target.value);
}

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: "" });
function onSubmitGuess() {
props.onGuess(Number(currentGuess));
setCurrentGuess("");
}

render() {
return (
<div>
<input
type="number"
value={this.state.currentGuess}
onChange={this.handleInputChange}
/>
<Button onClick={this.onSubmitGuess}>Submit Guess</Button>
</div>
);
}
return (
<div>
<input type="number" value={currentGuess} onChange={handleInputChange} />
<Button onClick={onSubmitGuess}>Submit Guess</Button>
</div>
);
}

GuessControl.propTypes = {
onGuess: PropTypes.func.isRequired,
};

// class GuessControlOld extends Component {
// constructor(props) {
// super(props);

// 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 });
// }

// 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: "" });
// }

// render() {
// return (
// <div>
// <input
// type="number"
// value={this.state.currentGuess}
// onChange={this.handleInputChange}
// />
// <Button onClick={this.onSubmitGuess}>Submit Guess</Button>
// </div>
// );
// }
// }

export default GuessControl;

150 changes: 95 additions & 55 deletions src/NumberGuessingGame.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { Component } from "react";
/* eslint-disable react/no-unescaped-entities */
// import React,
import { useState } from "react";
import GuessControl from "./GuessControl";
import GuessMessage from "./GuessMessage";
import GameOver from "./GameOver";
Expand All @@ -13,65 +15,103 @@ function getRandomNumber() {

const MAX_ATTEMPTS = 5;

class NumberGuessingGame extends Component {
constructor(props) {
super(props);

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);
}
function NumberGuessingGame() {
const [numberToGuess, setNumberToGuess] = useState(getRandomNumber);
const [numberOfGuesses, setNumberOfGuesses] = useState(0);
const [latestGuess, setLatestGuess] = useState(null);

handleGuess(guess) {
this.setState({
latestGuess: guess,
numberOfGuesses: this.state.numberOfGuesses + 1,
});
function handleGuess(guess) {
setLatestGuess(Number(guess));
setNumberOfGuesses(numberOfGuesses + 1);
}

handleReset() {
this.setState({
numberToGuess: getRandomNumber(),
numberOfGuesses: 0,
latestGuess: null,
});
function handleReset() {
setNumberToGuess(getRandomNumber());
setNumberOfGuesses(0);
setLatestGuess(null);
}

render() {
const isCorrectGuess = this.state.latestGuess === this.state.numberToGuess;

const isGameOver =
isCorrectGuess || this.state.numberOfGuesses === MAX_ATTEMPTS;

return (
<div>
<h2>I'm thinking of a number from 1 to 100.</h2>
<h2>
Can you guess the number I am thinking of in {MAX_ATTEMPTS} tries?
</h2>
<GuessControl onGuess={this.handleGuess} />
{isGameOver && (
<GameOver hasWon={isCorrectGuess} onReset={this.handleReset} />
)}
{!isGameOver && (
<GuessMessage
guess={this.state.latestGuess}
numberToGuess={this.state.numberToGuess}
numberOfGuesses={this.state.numberOfGuesses}
/>
)}
</div>
);
}
const isCorrectGuess = latestGuess === numberToGuess;
const isGameOver = isCorrectGuess || numberOfGuesses === MAX_ATTEMPTS;

return (
<div>
<h2>I'm thinking of a number from 1 to 100.</h2>
<h2>
Can you guess the number I am thinking of in {MAX_ATTEMPTS} tries?
</h2>
<GuessControl onGuess={handleGuess} />
{isGameOver && <GameOver hasWon={isCorrectGuess} onReset={handleReset} />}
{!isGameOver && (
<GuessMessage
guess={latestGuess}
numberToGuess={numberToGuess}
numberOfGuesses={numberOfGuesses}
/>
)}
</div>
);
}

// class NumberGuessingGameOld extends Component {
// constructor(props) {
// super(props);

// 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,
// });
// }

// handleReset() {
// this.setState({
// numberToGuess: getRandomNumber(),
// numberOfGuesses: 0,
// latestGuess: null,
// });
// }

// render() {
// const isCorrectGuess = this.state.latestGuess === this.state.numberToGuess;

// const isGameOver =
// isCorrectGuess || this.state.numberOfGuesses === MAX_ATTEMPTS;

// return (
// <div>
// <h2>I'm thinking of a number from 1 to 100.</h2>
// <h2>
// Can you guess the number I am thinking of in {MAX_ATTEMPTS} tries?
// </h2>
// <GuessControl onGuess={this.handleGuess} />
// {isGameOver && (
// <GameOver hasWon={isCorrectGuess} onReset={this.handleReset} />
// )}
// {!isGameOver && (
// <GuessMessage
// guess={this.state.latestGuess}
// numberToGuess={this.state.numberToGuess}
// numberOfGuesses={this.state.numberOfGuesses}
// />
// )}
// </div>
// );
// }
// }

export default NumberGuessingGame;