Skip to content

Commit a68eb6b

Browse files
committed
Fix and Local Storage - prblms
1 parent 0ecde66 commit a68eb6b

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

src/App.tsx

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import Signature from './components/Signature';
1111
const App: FC = () => {
1212

1313
const [toDoInput, setToDoInput] = useState<string>('');
14-
const [todos, setTodos] = useState<string[]>([]);
14+
const [todos, setTodos] = useState<{ text: string, completed: boolean, id: number }[]>([]);
1515
const [toDoPosition, setToDoPosition] = useState<string>('all');
16-
const [toDoFilter, setToDoFilter] = useState<string[]>([]);
16+
const [toDoFilter, setToDoFilter] = useState<{ text: string, completed: boolean, id: number }[]>([]);
1717
const [hasError, setHasError] = useState<boolean>(false);
1818
const [showError, setShowError] = useState<boolean>(false);
1919

@@ -37,39 +37,29 @@ const App: FC = () => {
3737

3838
}
3939

40-
useEffect( ()=> {
41-
42-
ToDoFilterHandler();
43-
ToDoSaveLocalStorage();
4440

45-
}, [todos, toDoPosition]);
41+
//Trying to get data from local storage
4642

4743
useEffect( ()=> {
4844

49-
ToDoRetrieveLocalStorage();
45+
const data = window.localStorage.getItem('todos');
46+
if (data !== null) setTodos(JSON.parse(data));
5047

5148
}, [])
5249

5350

51+
//Trying to save data to local storage
5452

55-
const ToDoSaveLocalStorage = function (): void {
56-
57-
localStorage.setItem('todos', JSON.stringify(todos));
58-
59-
};
53+
useEffect( ()=> {
6054

61-
const ToDoRetrieveLocalStorage = (): any => {
55+
ToDoFilterHandler();
56+
window.localStorage.setItem('todos', JSON.stringify(todos));
6257

63-
if (localStorage.getItem('todos') === null) {
64-
localStorage.setItem('todos', JSON.stringify([]));
65-
}
58+
}, [todos, toDoPosition]);
6659

67-
else {
68-
let storedTodos = JSON.parse(localStorage.getItem('todos') as string);
69-
if(storedTodos) setTodos(storedTodos);
70-
}
60+
7161

72-
};
62+
7363

7464

7565

0 commit comments

Comments
 (0)