Be A React Dev

Game Action - User Decision Rock Paper Scissors

10-Jul-2023


Amazon Prime

typescript plus react

What you will be building

Please check this link: Rock Paper Scissors Game

This app uses components from Chakra UI . You should check it out.

Reference

We first looked at setDecisionUser here.

actions/set-decision-user.ts

//  local utilities
import { SetState, State } from '../state/interfaces';

interface Payload {
  decision: string;
  setState: SetState;
}

const setDecisionUser = (payload: Payload) => {
  const { decision, setState } = payload;

  setState((prevState: State) => ({
    ...prevState,
    decision: {
      ...prevState.decision,
      user: decision,
    },
  }));
};

export default setDecisionUser;

But What Does It Do?

The function is fairly simple. It accepts a payload. The payload is typed by a TypeScript interface called Payload. We import SetState and State from state/interfaces. We first looked at the interfaces for SetState and State here.

setState calls an internal react function which is used to modify state. We need to modify the user decision so we grab the previous state (declared with the name of prevState) and return the modified version which is most of the prevState as is, and then the decision is modified, but we want some of the prevState decision to be returned as is, and the user portion is changed to whatever the user decided.

And job done!


Get Cool Swag

If you are finding this content useful (or maybe you are just a nice person or maybe you just like my merch) I would be greatful if you headed over to my shop and make a purchase or two. All proceeds will go towards making more courses.

If there’s merch missing that you would like, let me know (click this: Make Something For Me ) and I’ll try to make it for you.

It Is Because I Am Autistic T-Shirt

It Is Because I Am Autistic T-Shirt

You can buy this product or you can check out my shop of products.


About

The Author Guy

Find Me

Rob Welan <rob.welan@beareact.dev>FacebookKo-FiLinkedInMediumPatreonRSS FeedStack OverflowX

Tech In Use

Be A React Dev

© 2024 Be A React Dev. All rights reserved.