On a custom component, if I dispatch a new value by onPress function it works. If I set it into a setInterval or Background-Timer, the new value is not updated.
Reducer:
import initialState from './InitialState';
export function setData(type, data) {
if (!data) var data = '';
return {
type: type,
state: data
}
}
const reducer = (state = initialState, action) => {
console.log("action che arriva al reducer", action)
switch (action.type) {
case 'setInitialState':
return initialState
case 'setTipology':
return {
...state, typology: action.state
}
case 'setTimer':
return {
...state, timer: action.state
}
}
return state;
}
export default reducer
Custom Component :
const Timer = ({
title,
action
}) => {
const dispatch = useDispatch()
const reduxState = useSelector(state => state)
const timerFunction = (action) => {
if (action == "start") {
//dispatch(setData('setTimer', reduxState.timer + 1000))
BackgroundTimer.runBackgroundTimer(() => {
dispatch(setData('setTimer', reduxState.timer + 1000))
}, 1000);
} else if (action == "end") {
return BackgroundTimer.stopBackgroundTimer(); //after this call all code on background stop run.
}
}
return (
<>
<TouchableOpacity
onPress={() => timerFunction(action)}
style={styles.cButton}
>
<Text style={styles.testo}>{title}</Text>
</TouchableOpacity>
</>
)
}
If I use ONLY the commented line it works:
( dispatch(setData('setTimer', reduxState.timer + 1000)) )
In this example didn't work. Any suggestions?