处理导航以提醒未保存的更改

时间:2019-07-13 17:35:39

标签: javascript react-native react-navigation expo react-navigation-stack

我正在使用来自react-navigation的defaultGetStateForAction来拦截我的导航,因此我可以提醒我的用户有未保存的数据,并为他提供继续浏览并删除未保存的数据或先保存它的选项。这些数据存储到更高的组件状态,可以通过screenProps可用的功能对其进行访问

所以...这是我的代码:

const CreateAnimalStack = createStackNavigator({
    CreateAnimal: { screen: createAnimalScreen },
    ListAnimals: { screen: listAnimalsScreen },
    DetailAnimal: { screen: detailAnimalScreen },
    ReviewAnimal: { screen: reviewAnimalScreen },
    ListMarks: { screen: listMarksScreen },
    CreateMark: { screen: createMarkScreen },
}, {
        initialRouteName: 'CreateAnimal',
    });

const defaultGetStateForAction = CreateAnimalStack.router.getStateForAction;

CreateAnimalStack.router.getStateForAction = (action, state) => {
    if (
        state &&
        action.type === NavigationActions.BACK &&
        state.routeName === 'CreateAnimal' &&
        state.routes[state.index].routeName === 'ReviewAnimal' &&
        state.index === 1
    ) {
        Alert.alert(
            'Alert Title',
            'My Alert Msg',
            [
                { text: 'Ask me later', onPress: () => console.log('Ask me later pressed') },
                {
                    text: 'Cancel',
                    onPress: () => {
                        return null;
                    },
                    style: 'cancel',
                },
                {
                    text: 'OK', onPress: () => {
                        const routes = [
                            { routeName: 'CreateAnimal' },
                        ];
                        return {
                            ...state,
                            routes,
                            index: 0,
                        };
                    }
                },
            ],
            { cancelable: false },
        );
        return state;

    } 
    return defaultGetStateForAction(action, state);
};

export default CreateAnimalStack;

1 个答案:

答案 0 :(得分:0)

我认为这里的主要问题是Alert未被阻止,因此导航仍会在警报后进行。

话虽如此,您应该从react-navigation documentationnull语句中返回if:它将取消导航。

if (...) {
  Alert.alert(
      'Alert Title',
      'My Alert Msg',
      // ...
  );

  return null;
}

然后以“警报OK”按钮分辨率,您必须重新调度导航操作(而不是返回状态),但是问题是您没有dispatchdispatch来自AppContainer。一种方法是创建一个NavigationService来存储导航器,例如described in the doc,然后从dispatch导出它。

// ...
{
    text: 'OK', onPress: () => {
        const action = NavigationActions.navigate({ routeName: 'CreateAnimal', params: {}});

        // here you have to call dispatch from the AppContainer navigator (NavigationService ?)
        NavigationService.dispatch(action);
    }
},

所有这些代码在我看来都是脆弱而丑陋的。

我建议您首先探索一种完全不同的方法,例如,使用自定义的onPress回调用屏幕headerLeft替换默认的navigationOptions组件,而不是监听状态变化。

这是一个简单的未经测试的示例,该示例从react-navigation导入默认的左键。

import { HeaderBackButton } from 'react-navigation';

class YourScreen extends Component {
  static navigationOptions = ({ navigation }) => {
    const onBack = {
      Alert.alert(
        'Alert Title',
        'My Alert Msg',
        [
            { text: 'Ask me later', onPress: () => console.log('Ask me later pressed') },
            {
                text: 'Cancel',
                style: 'cancel',
            },
            {
                text: 'OK', onPress: () => {
                    navigation.goBack();
                }
            },
        ],
        { cancelable: false }
      );
    }

    return {
      headerLeft: <HeaderBackButton  onPress={() => onBack()} />
    }
}