Skip to content

Commit b73eed2

Browse files
author
Pankaj Sharma
committed
fix state update async
1 parent fa6d81b commit b73eed2

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/App.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import './App.css';
2-
import { useState } from 'react';
2+
import { useEffect, useState } from 'react';
33

44
function App() {
55

@@ -53,14 +53,17 @@ function App() {
5353
// update state
5454
setPreferences(updatedPreference);
5555

56+
}
57+
58+
// check preferences are updated then we will check if all check box are selected then mark select all else mark false
59+
useEffect(() => {
60+
5661
// we also check if all items are selected
5762
let allChecked = preferences.every(preference => preference.selected === true);
5863

59-
if (allChecked === true) {
60-
setAllFav(allChecked);
61-
}
62-
63-
}
64+
setAllFav(allChecked);
65+
66+
}, [preferences])
6467

6568

6669
return (

0 commit comments

Comments
 (0)