Code Monkey home page Code Monkey logo

react-samples-2021's Introduction

React TODO Sample Remix 2021

  • This is a React classic sample TODO app in a current 2021 way.
  • tried to be a real world example includes backend API and tests.
  • using the latest version, tools and practices as of 2021.03.
    • TypeScript
    • Function Component
    • React Hooks (useState, useReducer, useRef, createContext, custom hook)
    • Material-UI and makeStyles (not App.css)
    • Tests with Jest and React Testing Library (not Enzyme)
    • Mock Service Worker
  • remixed official docs and best practices found in various blogs.
  • hoping to be a simple hello world example for reference.

Key Points Summary

  • useReducer is similar to useState but for complex state
  • move logics from view to custom hooks and reducer if needed
  • use the latest tools (RTL, MSW) for readable test

Setup

# https://github.com/facebook/create-react-app
npx create-react-app todo-ts --template typescript
cd todo-ts

# https://material-ui.com/getting-started/installation/
# https://material-ui.com/guides/typescript/
npm install @material-ui/core @material-ui/icons @material-ui/lab
# add Roboto Font to public/index.html in the line before manifest

npm install axios
npm install json-server msw --save-dev

Test and Run

# run backend server (note: waitFor default timeout = 1000ms)
npx json-server --delay 500 db.json
# run app
PORT=4000 BROWSER=none npm start
open http://localhost:4000

# run test
npm test
# run test only one file
npm test -- App.test.tsx
# test with coverage
npm test -- --coverage
open coverage/lcov-report/index.html
# https://facebook.github.io/create-react-app/docs/running-tests

# build for production
npm run build
# https://facebook.github.io/create-react-app/docs/deployment

Backend Json Server

  • Json Server is a simple backend server for development
# start
npx json-server db.json

# http://localhost:3000/db
# http://localhost:3000/todo
# http://localhost:3000/todo/1

# todo by curl
# create (id is auto increment)
curl -X POST -H 'Content-Type: application/json' \
  --data-binary '{"title": "Foo", "done": false}' \
  --dump-header /dev/stdout \
  http://localhost:3000/todo
# update
curl -X PUT -H 'Content-Type: application/json' \
  --data-binary '{"id": 3, "title": "Foo", "done": true}' \
  --dump-header /dev/stdout \
  http://localhost:3000/todo/3
# delete
curl -X DELETE \
  --dump-header /dev/stdout \
  http://localhost:3000/todo/3

App Structure

  • App (tasks)
    • TaskInput {createTask}
    • TaskList {tasks}
      • TaskItem {task, updateTask, deleteTask}

Reference

about App

about Test

react-samples-2021's People

Contributors

kenfj avatar

Watchers

 avatar  avatar

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.