简体   繁体   English

使用“Redux-Saga”中的“React Navigation 6.0”重定向用户

[英]Redirecting user using 'React Navigation 6.0' inside 'Redux-Saga'

I am using React Navigation 6.0 and I want to push or redirect the user after a specific function was called out.我正在使用React Navigation 6.0 ,我想在调用特定功能后推送或重定向用户。

Inside my redux saga function:在我的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)
    }

Notice that I've used :请注意,我已经使用了:

yield put(NavigationActions.navigate({routeName: 'ProfilePageScreen'}));产量放置(NavigationActions.navigate({routeName:'ProfilePageScreen'}));

to try to redirect the user once the token is saved using AsyncStorage .尝试在使用AsyncStorage保存令牌后重定向用户。

However, this doesn't work.但是,这不起作用。 How do I redirect my user after the token saved inside redux saga ?redux saga保存令牌后如何重定向我的用户? Please help!请帮忙!

UPDATE : I tried both of the following as per React Navigation 6 .更新:我根据React Navigation 6尝试了以下两种方法。

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

and also:并且:

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

But both have no effect.但是两者都没有效果。

Change routeName to name and tryrouteName更改为name并尝试

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

and there is more if you are switching from React Navigation 4 to React Navigagtion 6如果你从React Navigation 4切换到React Navigagtion 6 ,还有更多

Now NavigationActions are deprecated and shift to another package called @react-navigation/native and from there you can call commonActions and from there you can navigate.现在NavigationActions已被deprecated并转移到另一个名为@react-navigation/native ,从那里你可以调用commonActions并从那里你可以导航。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM