-
Notifications
You must be signed in to change notification settings - Fork 80
Spruce - Ngozi Amaefule #74
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work Ngozi! You met all the learnings goals and passed the tests for this project.
I added ways to refactor and other approaches in your PR.
Keep up the great work!
Project Grade: 🟢
@@ -12,7 +12,6 @@ | |||
"plugin:react/recommended", | |||
"plugin:jsx-a11y/recommended", | |||
"plugin:react-hooks/recommended", | |||
"plugin:jest/recommended", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume jest
was removed from VS Code because it was running tests with every change right?
We actually turn off auto run by adding the key-value pair to our settings.json
in VS Code:
"jest.autoRun": "off"
// Wave 2 | ||
// You will need to create a method to change the square | ||
// When it is clicked on. | ||
// Then pass it into the squares as a callback | ||
|
||
const onClickSquare = (id) => { | ||
console.log(id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When pushing code to be reviewed, we should eliminate any print statements or console logs.
console.log(id); |
// Wave 2 | ||
// You will need to create a method to change the square | ||
// When it is clicked on. | ||
// Then pass it into the squares as a callback | ||
|
||
const onClickSquare = (id) => { | ||
console.log(id); | ||
const newSquares = [...squares]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job in making a copy of the squares data! Here's a reminder on why we need to make the copy:
To trigger the update
stage for this component, React needs to notice a change in state data. We could mutate state directly, however, this doesn't scale well and doesn't follow React best practices.
Instead, we should pass in a copy of our state data to the setState function setSquares()
which will have React compare the object references between the copy and the old state data. If the object references are different, React will then trigger the update
stage.
const onClickSquare = (id) => { | ||
console.log(id); | ||
const newSquares = [...squares]; | ||
if (checkForWinner() === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The !
(not) operator can also be used to check if a value is falsy
if (checkForWinner() === null) { | |
if (!checkForWinner()) { |
if (currentPlayer === Player1) { | ||
setCurrentPlayer(Player2); | ||
} else { | ||
setCurrentPlayer(Player1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use a ternary operator to set the currentPlayer:
if (currentPlayer === Player1) { | |
setCurrentPlayer(Player2); | |
} else { | |
setCurrentPlayer(Player1); | |
} | |
setCurrentPlayer(currentPlayer === PLAYER_1 ? PLAYER_2 : PLAYER_1; | |
const Square = (props) => { | ||
// For Wave 1 enable this | ||
// Component to alert a parent | ||
// For Wave 1 enable this | ||
// Component to alert a parent | ||
// component when it's clicked on. | ||
const clickHandler = (e) => { | ||
e.preventDefault(); | ||
props.onClickCallback(props.id); | ||
}; | ||
|
||
return <button | ||
className="square" | ||
> | ||
{props.value} | ||
</button> | ||
} | ||
return ( | ||
<button className="square" onClick={clickHandler}> | ||
{props.value} | ||
</button> | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use object destructuring to reference prop data by their key name instead.
const Square = ( {value, id, onClickCallback, key} ) => {
const clickHandler = (e) => {
e.preventDefault();
onClickCallback(id);
};
return (
<button className="square" onClick={clickHandler}>
{value}
</button>
);
No description provided.