Skip to content

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

ngoziamaefule
Copy link

No description provided.

Copy link

@audreyandoy audreyandoy left a 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",

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

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.

Suggested change
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];

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) {

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

Suggested change
if (checkForWinner() === null) {
if (!checkForWinner()) {

Comment on lines +55 to +59
if (currentPlayer === Player1) {
setCurrentPlayer(Player2);
} else {
setCurrentPlayer(Player1);
}

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:

Suggested change
if (currentPlayer === Player1) {
setCurrentPlayer(Player2);
} else {
setCurrentPlayer(Player1);
}
setCurrentPlayer(currentPlayer === PLAYER_1 ? PLAYER_2 : PLAYER_1;

Comment on lines 6 to +20
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>
);
};

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants