首页 > 解决方案 > 在“Redux-Saga”中使用“React Navigation 6.0”重定向用户

问题描述

我正在使用React Navigation 6.0并且我想在调用特定功能后推送或重定向用户。

在我的redux saga功能内部:

    export function* Login({payload}) {
      try {
          const response = yield call(loginUser, payload);
          yield call(setToken(response.token));
          yield put(NavigationActions.navigate({routeName: 'ProfilePageScreen'}));
      } catch (error) {
       console.log(error)
    }

请注意,我使用过:

yield put(NavigationActions.navigate({routeName: 'ProfilePageScreen'}));

使用 . 保存令牌后尝试重定向用户AsyncStorage

但是,这不起作用。令牌保存在里面后如何重定向我的用户redux saga?请帮忙!

更新:我根据React Navigation 6尝试了以下两种方法。

  yield call(
    navigation.dispatch(
      CommonActions.navigate({
        name: 'ProfileScreen',
      }),
    ),
  );

并且:

  yield put(
    navigation.dispatch(
      CommonActions.navigate({
        name: 'ProfileScreen',
      }),
    ),
  );

但两者都没有效果。

标签: reactjsreact-nativereact-navigation

解决方案


更改并routeName尝试name

yield put(NavigationActions.navigate({name: 'ProfilePageScreen'}));

如果您要从 切换React Navigation 4React Navigagtion 6

现在转移到另一个调用的包NavigationActions,从那里你可以打电话,从那里你可以导航。deprecated@react-navigation/nativecommonActions

文档:https ://reactnavigation.org/docs/navigation-actions


推荐阅读