@@ -11,9 +11,9 @@ import Signature from './components/Signature';
11
11
const App : FC = ( ) => {
12
12
13
13
const [ toDoInput , setToDoInput ] = useState < string > ( '' ) ;
14
- const [ todos , setTodos ] = useState < string [ ] > ( [ ] ) ;
14
+ const [ todos , setTodos ] = useState < { text : string , completed : boolean , id : number } [ ] > ( [ ] ) ;
15
15
const [ toDoPosition , setToDoPosition ] = useState < string > ( 'all' ) ;
16
- const [ toDoFilter , setToDoFilter ] = useState < string [ ] > ( [ ] ) ;
16
+ const [ toDoFilter , setToDoFilter ] = useState < { text : string , completed : boolean , id : number } [ ] > ( [ ] ) ;
17
17
const [ hasError , setHasError ] = useState < boolean > ( false ) ;
18
18
const [ showError , setShowError ] = useState < boolean > ( false ) ;
19
19
@@ -37,39 +37,29 @@ const App: FC = () => {
37
37
38
38
}
39
39
40
- useEffect ( ( ) => {
41
-
42
- ToDoFilterHandler ( ) ;
43
- ToDoSaveLocalStorage ( ) ;
44
40
45
- } , [ todos , toDoPosition ] ) ;
41
+ //Trying to get data from local storage
46
42
47
43
useEffect ( ( ) => {
48
44
49
- ToDoRetrieveLocalStorage ( ) ;
45
+ const data = window . localStorage . getItem ( 'todos' ) ;
46
+ if ( data !== null ) setTodos ( JSON . parse ( data ) ) ;
50
47
51
48
} , [ ] )
52
49
53
50
51
+ //Trying to save data to local storage
54
52
55
- const ToDoSaveLocalStorage = function ( ) : void {
56
-
57
- localStorage . setItem ( 'todos' , JSON . stringify ( todos ) ) ;
58
-
59
- } ;
53
+ useEffect ( ( ) => {
60
54
61
- const ToDoRetrieveLocalStorage = ( ) : any => {
55
+ ToDoFilterHandler ( ) ;
56
+ window . localStorage . setItem ( 'todos' , JSON . stringify ( todos ) ) ;
62
57
63
- if ( localStorage . getItem ( 'todos' ) === null ) {
64
- localStorage . setItem ( 'todos' , JSON . stringify ( [ ] ) ) ;
65
- }
58
+ } , [ todos , toDoPosition ] ) ;
66
59
67
- else {
68
- let storedTodos = JSON . parse ( localStorage . getItem ( 'todos' ) as string ) ;
69
- if ( storedTodos ) setTodos ( storedTodos ) ;
70
- }
60
+
71
61
72
- } ;
62
+
73
63
74
64
75
65
0 commit comments