React native React Redux不更新SetInterval上的值
在自定义组件上,如果我通过React native React Redux不更新SetInterval上的值,react-native,redux,react-redux,React Native,Redux,React Redux,在自定义组件上,如果我通过onPress函数分派一个新值,它就会工作。如果我将其设置为设置间隔或后台计时器,则新值不会更新 减速器: import initialState from './InitialState'; export function setData(type, data) { if (!data) var data = ''; return { type: type, state: data } } const red
onPress
函数分派一个新值,它就会工作。如果我将其设置为设置间隔
或后台计时器
,则新值不会更新
减速器:
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
自定义组件:
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>
</>
)
}
在这个例子中,它不起作用。
有什么建议吗
( dispatch(setData('setTimer', reduxState.timer + 1000)) )