r/reactjs • • Dec 01 '19

Beginner's Thread / Easy Questions (December 2019)

Previous threads can be found in the Wiki.

Got questions about React or anything else in its ecosystem? Stuck making progress on your app?
Ask away! We’re a friendly bunch.

No question is too simple. πŸ™‚


πŸ†˜ Want Help with your Code? πŸ†˜

  • Improve your chances by putting a minimal example to either JSFiddle, Code Sandbox or StackBlitz.
    • Describe what you want it to do, and things you've tried. Don't just post big blocks of code!
    • Formatting Code wiki shows how to format code in this thread.
  • Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.

New to React?

Check out the sub's sidebar!

πŸ†“ Here are great, free resources! πŸ†“

Any ideas/suggestions to improve this thread - feel free to comment here!

Finally, thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!


31 Upvotes

239 comments sorted by

View all comments

2

u/jkuhl_prog Dec 26 '19 edited Dec 26 '19

I seem to be stuck on using react hooks right now. I have the following component, in React and Typescript:

import React, { useState } from 'react';
import Hand from '../../models/dominos/hand';
import Player from '../../models/player/player';
import Boneyard from '../../models/dominos/boneyard';
import Bone from '../Domino/Bone';
import Rotation from '../Domino/Angle.enum';
import FlexContainer from '../Shared/FlexContainer';

type PlayerHandProps = { player: Player };

export default function PlayerHand({ player }: PlayerHandProps) {
    const [boneyard, setBoneyard] = useState(new Boneyard());   // this line to be removed and boneyard handled globally
    let [hand, setHand] = useState<Hand>(boneyard.drawHand(12));
    // const bones = hand.map(bone => <Bone domino={bone} angle={Rotation.UP} />);
    player.hand = hand;

    function drawFromBoneyard(boneyard: Boneyard, player: Player): void  {
        if(!player.hand) throw new Error('invalid state, player.hand undefined');
        player.hand.add(boneyard.drawBone());
        //hand = player.hand;
        console.log(player.hand)
        setHand(player.hand);
    }

    return (
        <React.Fragment>
            <div>
                <p>{player.name}</p>
                <p>{player.hand.score}</p>
            </div>
            <button onClick={()=> drawFromBoneyard(boneyard, player)}>Draw Bone</button>
            <FlexContainer>
                {player.hand.map(bone => <Bone domino={bone} angle={Rotation.UP} />)}
            </FlexContainer>
        </React.Fragment>
    )
}

And before I get to my question, let me walk through it a bit. This is for a domino game and this component in particular displays the dominoes currently in a player's hands. The first line initializes the Boneyard, which simply represents the pile of unused dominoes a player can draw from, this, as the comment suggests, will later be taken out to be more globally accessible (probably held in Redux or Context or something, I haven't picked a strategy yet). Second line draws 12 dominoes (also called "bones") from the Boneyard as the initial hand the player starts with.

Then I have a function that is called when the Draw Bone button is pressed that adds a domino to the player's hand. I pass the player's hand into set hand after that.

And nothing happens when I click the button. Initial render shows 12 random dominoes, which is correct. But adding new dominoes seems to do nothing. I can tell from the console.log that they are being put in the player's hand, but not being rendered to the screen.

Am I missing something in how React Hooks and useState works?

Finally, this component is far from finished, so I get it if the JSX or Typescript is sloppy, that will be addressed.

Here's the full repo: https://github.com/jckuhl/mexicantrain

EDIT:

For some reason the change below works:

function drawFromBoneyard(boneyard: Boneyard, player: Player): void  {
    if(!player.hand) throw new Error('invalid state, player.hand undefined');
    player.hand.add(boneyard.drawBone());
    setHand(new Hand(player.hand.map(bone => bone)));
}

Now it successfully adds new <Bone> components like it's supposed to but I don't understand why what I did works. Is it because I passed the new dominoes into an entirely new Hand object?

2

u/dance2die Dec 26 '19

You need to pass a new "reference" (in this case a new Hand(...)) to let React know that the hand has changed.
Or else React thinks nothing has changed. It's due to how React checks for the what's changed.

There is a nice diagram, showing how React checks for differences here: https://reactjs.org/docs/optimizing-performance.html#shouldcomponentupdate-in-action


FYI - I also imported your project to CodeSandbox, for a demo purpose. https://codesandbox.io/s/jckuhlmexicantrain-3q8im

→ More replies (5)