我正在使用react-navigation,並且想要在用戶點擊/快速點擊按鈕時避免兩次導航到同一屏幕。這裏是我的減速器:快速點擊按鈕時避免導航兩次
export const navReducer = (state = initialState, action = {}) => {
let nextState;
switch (action.type) {
case TO_LOGIN:
nextState = RootNav.router.getStateForAction(
NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({
type: NavigationActions.NAVIGATE,
routeName: TO_LOGIN
})],
key: null
}), state);
break;
case TO_HOME:
nextState = RootNav.router.getStateForAction(
NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({
type: NavigationActions.NAVIGATE,
routeName: TO_HOME
})],
}), state);
break;
default:
if (action.type === NavigationActions.NAVIGATE) {
console.log('action: ' + JSON.stringify(action));
console.log('state: ' + JSON.stringify(state));
console.log('nextState: ' + JSON.stringify(RootNav.router.getStateForAction(action, state)));
}
nextState = RootNav.router.getStateForAction(action, state);
break;
}
return nextState || state;
};
的console.logs的輸出是:
首先點擊:
action: {"type":"Navigation/NAVIGATE","routeName":"ClinicDetail","params":{"section":0,"from":"near"}}
state: {"index":0,"routes":[{"routeName":"TO_HOME","key":"id-1496294907150-4"}]}
nextState: {"index":0,"routes":[{"routeName":"TO_HOME","key":"id-1496294907150-4"}]}
第二點擊:
action: {"type":"Navigation/NAVIGATE","routeName":"ClinicDetail","params":{"section":0,"from":"near"}}
state: {"index":0,"routes":[{"routeName":"TO_HOME","key":"id-1496294907150-4"}]}
nextState: {"index":0,"routes":[{"routeName":"TO_HOME","key":"id-1496294907150-4"}]}
什麼樣的檢查,以這樣做我可以防止這種情況發生?