Be A React Dev

Game View - Game Results Rock Paper Scissors

03-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.

Let’s get right into it. Here is the file you will need.

view/results.tsx

import React from 'react';
//  chakra-ui
import { HStack, VStack, Text } from '@chakra-ui/react';
//  interfaces
import { State } from '../state/interfaces';

interface Props {
  state: State;
}

const DisplayResults = (props: Props) => {
  const { state } = props;

  return (
    <HStack
      maxW="sm"
      borderWidth="1px"
      borderRadius="lg"
      overflow="hidden"
      sx={{ marginBottom: '1em !important', marginTop: '1em !important' }}
    >
      <VStack p={4}>
        <Text>Computer:</Text>
        <Text>{state.games.computer}</Text>
      </VStack>

      <VStack p={4}>
        <Text>Games:</Text>
        <Text>{state.games.total}</Text>
      </VStack>

      <VStack p={4}>
        <Text>User:</Text>
        <Text>{state.games.user}</Text>
      </VStack>
    </HStack>
  );
};

export default DisplayResults;

Imports

First we import React of course. We also need components from Chakra-UI. And we need our typing for State which we defined here.

Interface & Function

Next we define the TypeScript interface and the Typed function function using a fat arrow method, and pass in props from the parent component. The props will only contain state and are typed by our interface of Props above.

The component returns an HStack (a horizontal stack of things) and the HStack has been given a nice border and formatting. The HStack contains a set of VStacks each with a padding of 4. It’s a thing.

Within each VStack are two paragraphs (Text). These columns represent computer wins, total games played and user wins.

And that is it!


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.

Wistful T-Shirt

Wistful 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.