YT-Code Genix: React Query Tutorial V5 - Full Tanstack Query Tutorial for Beginners -- https://www.youtube.com/watch?v=3e-higRXoaM
pnpm i @tanstack/react-query @tanstack/react-query-devtools
pnpm i axios react-hook-form
pnpm i @tanstack/eslint-plugin-query --D
- In the extends section:
plugin:@tanstack/eslint-plugin-query/recommended - In the plugins section:
"@tanstack/query"
- Start the backend server "prototype-backend-sever"
- Create "services/api.ts" to talk to backend - get all to do ids
- Create "types/types.d.ts"
- Create "services/queries.ts" - create a todo query function
- Create "services/components/TodoShow.tsx"
- Wrap the "App" in main.tsx inside the query client provider
- Use the query function in the component - TodoShow.tsx
- Fetch each todo one-by-one
- Create services/api.ts getTodo function
- Create services/queries.ts useTodos function - multiple parallel queries
-
You use query to get data from the server
-
You use mutations to change the state of the server
-
Create services/api.ts createTodo function
-
Create services/mutations.ts useCreateTodo function
-
Add createTodo mutation to TodoCreate.tsx
-
Create form submit handler
-
Create a form using react-hook-form
- We have to update the page to see the new list item added
- The onSettled funciton of the mutation is a good place to either invalidate or refetch a query
-
Update the onSettled function of the mutation to invalidate the query
-
Prevent submiting a new todo when the previous mutation is in progress. Also change the submit button text when creating.
-
When you create a todo you add a new item to our previous fetch todos list.
-
When updating you just update that single todo
-
Create api.ts updateTodo function
-
Create mutations.ts useUpdateTodo function
-
Add useUpdateTodo mutation in the TodoShow.tsx
- Add a button which shows "Done" or "Mark as done" based on the status of the "checked" field.
- Update todo status in server when this button is clicked
- Create api.ts deleteTodo function
- Create mutation.ts useDeleteTodo
- Add a delete button to TodoShow.tsx and add the delete mutation
- First write sync mutate, the write async mutate.
- Create a new component - Projects.tsx
- get projects and display with pagination - backend courtesy json-server
- Create a products component with api and query
- Use infiniteQuery
- Get the complete todo based on the proudct id clicked
- Optimse fetching with placeholderData