1
- import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query' ;
1
+
2
2
import { useRef } from 'react' ;
3
- import { Todo } from './hooks/useTodos' ;
4
- import axios from 'axios' ;
3
+ import useAddTodo from './hooks/useAddTodo' ;
5
4
6
5
const TodoForm = ( ) => {
7
- const queryClient = useQueryClient ( ) ;
8
- const addTodo = useMutation < Todo , Error , Todo > ( {
9
- mutationFn : ( todo : Todo ) =>
10
- axios
11
- . post < Todo > ( 'https://jsonplaceholder.typicode.com/todos' , todo )
12
- . then ( res => res . data ) ,
13
- onSuccess : ( savedTodo , newTodo ) => {
14
- // queryClient.invalidateQueries({
15
- // queryKey: ['todos']
16
- // })
17
- queryClient . setQueryData < Todo [ ] > ( [ 'todos' ] , todos => [ savedTodo , ...( todos || [ ] ) ] ) ;
18
-
19
- if ( ref . current ) ref . current . value = '' ;
20
- }
21
- } ) ;
22
6
const ref = useRef < HTMLInputElement > ( null ) ;
7
+ const addTodo = useAddTodo ( ( ) => {
8
+ if ( ref . current ) ref . current . value = '' ;
9
+ } )
23
10
return (
24
11
< >
25
12
{ addTodo . error && < div className = "alert alert-danger" >
@@ -40,8 +27,8 @@ const TodoForm = () => {
40
27
< input ref = { ref } type = "text" className = "form-control" />
41
28
</ div >
42
29
< div className = "col" >
43
- < button className = "btn btn-primary" disabled = { addTodo . isLoading } >
44
- { addTodo . isLoading ? 'Adding...' : ' Add' }
30
+ < button className = "btn btn-primary" >
31
+ Add
45
32
</ button >
46
33
</ div >
47
34
</ form >
0 commit comments