I'm coding an alarm app and want to count how many times the alarm has been triggered in an hour and in a day. I use redux toolkit to handle the state of the alarm (true/false) and the reducer countAlarm to increment the count. Right now handleCount dispatches countAlarm every time alarmValue is true. I would like to count how many times the alarmValue is true in an hour and in a day. How do I do that?
const dispatch = useDispatch();
const countValue = useSelector((state) => state.count.value);
const alarmValue = useSelector((state) => state.alarm.active);
const handleCount = () => {
if (alarmValue) {
dispatch(countAlarm());
}}
useEffect(() => {
handleCount()
}, [alarmValue])
return (
<View>
<Text>{countValue}</Text>
</View>
...
countAlarm:
import { createSlice } from '@reduxjs/toolkit';
const initialStateValue = {
value: 0
}
const countSlice = createSlice({
name: 'count',
initialState: initialStateValue,
reducers: {
countAlarm: (state = initialStateValue) => {
state.value++;
},
}
});
export const {countAlarm} = countSlice.actions;
export default countSlice.reducer;
alarmValue:
import { createSlice } from '@reduxjs/toolkit';
const initialStateValue = {
active: false,
}
const alarmSlice = createSlice({
name: 'alarm',
initialState: initialStateValue,
reducers: {
alarmOn: (state = initialStateValue) => {
state.active = true;
},
alarmOff: (state = initialStateValue) => {
state.active = false;
},
}
});
export const {alarmOn, alarmOff} = alarmSlice.actions;
export default alarmSlice.reducer;