Giter Club home page Giter Club logo

todo-app-with-json-server's Introduction

todo-app-with-json-server

Main concepts

  • API Rest, json-server y Postman

  • React app backend calls

  • Hook useCallback: it leaves the function reference frozen. When it's a function that parent component passes to a child component, we don't want it to be rendered on the child every time the parent changes. "Functions also have a reference". Example here

How to use it?

  • Run the local mocks server (json-server) en una nueva consola:

    cd mocks-server
    npm i
    npm start
    
  • In Postman test the Rest API of tasks that exposes the local mocks server:

    • GET /tasks: Gets the task list.
    • GET /tasks/:id: Gets the task with id id.
    • POST /tasks + body: Create a new task (the body goes without id).
    • PUT /tasks/:id + body: Update the task with id id(mashes the whole object)
    • PATCH /tasks/:id + body: Modifies certain properties of the task with id id.
    • DELETE /tasks/:id: Delete the task with id id.
    • COPY
    • HEAD
    • OPTIONS: related to CORS, it does not give you information from JavaScript.

React app backend calls, with load and error feedback.

  • The App component is prepared to display the appropriate visual components according to the different states that the interface may have:

    • Loading the task list
    • An error occurred while loading the task list.
    • Processing a request (add, delete or toggle if a task is done).
    • An error occurred while processing a request.
  • To make calls to the backend from the frontend, I use axios:

    npm i axios --save
    
  • The services/TaskListService.js file will contain the backend call logic.

  • The correct place to define the path to the base URL of the backend services is in an environment variable. It is possible to define environment variables in a project create-react-app in the file [.env] of the project root:

    They must be prefixed with REACT_APP_. For example

    REACT_APP_BASE_URL=http://localhost:3003
    

    Then, in the application code it is queried with:

    process.env.REACT_APP_BASE_URL
    
  • The result query returning a Promise, which is the type of data returned by the axios functions, must be done using async and await, along with try and catch.

  • The initial loading of the task list is implemented, giving feedback on the loading process, informing about possible errors and offering the possibility to retry.

todo-app-with-json-server's People

Contributors

lorellana21 avatar

Watchers

 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.