【问题标题】:Redirecting user using 'React Navigation 6.0' inside 'Redux-Saga'在“Redux-Saga”中使用“React Navigation 6.0”重定向用户
【发布时间】:2021-11-06 11:02:54
【问题描述】:

我正在使用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',
      }),
    ),
  );

但两者都没有效果。

【问题讨论】:

    标签: reactjs react-native react-navigation


    【解决方案1】:

    routeName改成name试试

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

    如果您从React Navigation 4 切换到React Navigagtion 6,还有更多

    现在 NavigationActionsdeprecated 并转移到另一个名为 @react-navigation/native 的包,然后您可以从那里调用 commonActions 并从那里导航。

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

    【讨论】:

    • 它没有重定向后令牌集。另外,我在控制台上看到了这个Required dispatch_sync to load constants for RNGestureHandlerModule. This may lead to deadlocks
    • 你使用的是哪个 react 导航版本?
    • React Navigation 6 是我使用的版本
    • 即便如此,我该如何使用 React Navigation 6 来做到这一点?
    • 你从哪里得到NavigationActions
    猜你喜欢
    • 2021-12-22
    • 2020-10-30
    • 2018-02-25
    • 2020-12-24
    • 2019-12-30
    • 2021-05-25
    • 2017-05-14
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多