美文网首页
关于react-navigation的goBack(key)

关于react-navigation的goBack(key)

作者: 码代码的小公举 | 来源:发表于2018-05-28 17:34 被阅读62次

这里是旧版本的一个goBack的问题。
1.随机的key,无法获取想要跳转的key
解决:获得你需要返回的key,从最外层通过screenProps把整个路由堆栈(props.navigation.state.routes)传进去,遍历出你想要返回的key。
2。传了key,跳转不正确。
问题:
找到node_modules下的react-navigation/src/routers/StackRouter.js
看到以下代码

if (action.type === NavigationActions.BACK) {
        let backRouteIndex = null;
        if (action.key) {
          const backRoute = state.routes.find(
            /* $FlowFixMe */
            (route: *) => route.key === action.key
          );
          /* $FlowFixMe */
          backRouteIndex = state.routes.indexOf(backRoute);
          //这里得到的是index,第一的其实为0
        }
        if (backRouteIndex == null) {
          return StateUtils.pop(state);
        }
        if (backRouteIndex > 0) {
          return {
            ...state,
            routes: state.routes.slice(0, backRouteIndex),  
            index: backRouteIndex - 1,
          };
        }
        //增加为0的时候
        if (backRouteIndex === 0) {
          return {
            ...state,
            routes: state.routes.slice(0, 1),  
            index: 0,
          };
        }
      }
      return state;
    },

这样就可以完成返回了,ps,新版本的不一样了哦,这就旧工程。

相关文章

网友评论

      本文标题:关于react-navigation的goBack(key)

      本文链接:https://www.haomeiwen.com/subject/myggjftx.html