-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLightBox.jsx
43 lines (39 loc) · 1.18 KB
/
LightBox.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const useState = React.useState;
const LightBox = (props) => {
const [switchValues, setSwitchValues] = useState(new Array(props.numOfSwitches).fill(false));
const toggleAllLights = () => {
if (switchValues.every((light) => light === false)) {
setSwitchValues(new Array(props.numOfSwitches).fill(true));
} else {
setSwitchValues(new Array(props.numOfSwitches).fill(false));
}
};
const toggleLight = (id) => {
setSwitchValues((prevSwitches) => {
const newSwitches = [...prevSwitches];
newSwitches[id] = !prevSwitches[id];
return newSwitches;
});
};
return (
<div>
<p>Click to Toggle a Light</p>
<p>Double Click to Toggle All Lights</p>
<div className="light-box">
{
switchValues.map((value, index) => {
return (
<LightSwitch
key={index} // This is bad practice. Read here for more: https://reactjs.org/docs/lists-and-keys.html
id={index}
status={value}
toggleLight={toggleLight}
toggleAllLights={toggleAllLights}
/>
);
})
}
</div>
</div>
);
};