Git Product home page Git Product logo

trivia-demo's Introduction

trivia-demo

Screenshot

A trivia app demo using React, TypeScript, Redux, React Router, React Transition Group and Jest

This is a demo app I created as an example of my work. Feel free to browse the code, try the live app, or fork the repo.

Available Scripts

In the project directory, you can run:

yarn start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

yarn test

Launches the test runner in the interactive watch mode.

yarn build

Builds the app for production to the build folder.
It bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.
The app is ready to be deployed!

trivia-demo's People

Contributors

localjo avatar

Stargazers

 avatar Timothy Mwirabua avatar  avatar Nishkar Ramautar avatar Maina James avatar Gabe Herbert avatar Vince Fulco--Bighire.tools avatar Youssef avatar  avatar Rossi Meacham avatar Theodros Zelleke avatar Michael D avatar wolfi3 avatar  avatar

Watchers

 avatar  avatar

trivia-demo's Issues

Code review notes

Hi, I'm a Redux maintainer. A few quick thoughts after skimming through the code:

  • First, given the current size of the app, Redux is definitely not necessary here - this could easily be done with plain React state. Nothing wrong with choosing to use Redux to get some practice with it, just pointing out that the data management isn't overly complex atm.
  • The reducer logic is currently mutating the state. In particular, q.is_correct = correctBool === action.payload.answer; is mutating q. Per the rules of immutable updates, you need to copy every level of nesting that is being updated.
  • Third, I'd strongly suggest using our new official Redux Toolkit package, which contains utilities to simplify your Redux logic. In this case, it would allow you to remove the action-types.ts and actions/index.ts files entirely, because it can generate those for you automatically based on your reducers:
const initialState: IAppState = {
  status: IStatus.INIT,
  questions: [],
};

const questionsSlice = createSlice({
    name: "questions",
    initialState,
    reducers: {
        setStatus(state, action: PayloadAction<IStatus>) {
            state.status = action.payload;
            if(action.payload === IStatus.INIT) {
                state.questions = [];
            }
        },
        addQuestions(state, action: PayloadAction<IQuestion[]>) {
            state.questions = action.payload;
        },
        submitAnswer(state, action: PayloadAction<{question: IQuestion, answer: string}>) {
            const matchingQuestion = state.questions.find(q => q.question === action.payload.question);
            if(matchingQuestion && matchingQuestion.type === 'boolean') {
                const {correct_answer} = matchingQuestion;
                const correctBool = /true/i.test(correct_answer);
                // Can "mutate" the state here because createSlice uses Immer inside
                q.is_correct = correctBool;
            }
            else {
				q.is_correct = false;
            }
        }
    }
})

export const {setStatus, addQuestions, submitAnswer} = questionsSlice.actions;
export default questionsSlice.reducer;

More code review suggestions

A few more quick suggestions after seeing you link this in Reactiflux:

  • You shouldn't need to declare the returned value type for useSelector usage - TS can infer it. Example usage:
const status = useSelector( (state: IAppState) => state.status);
 async function fetchData() {
    if (status === IStatus.INIT) {
      dispatch(setStatus(IStatus.LOADING));
      const res = await fetch(
        'https://opentdb.com/api.php?amount=10&difficulty=hard&type=boolean'
      );
      // use await for this
      const data= await res.json() as WhateverDataType;
      dispatch(addQuestions(data.results));
      // don't dispatch a separate "set status" action - have the status reducer listen for "add questions" too.
    }
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.