Sal*_*.O. 5 react-native react-navigation
这是根导航器
export const AppNavigator = StackNavigator({
Splash: { screen: Splash },
Dashboard: { screen: DashboardDrawer }
});
const DashboardDrawer = DrawerNavigator({ DashboardScreen: {
screen: StackNavigator({
A: { screen: A },
B: { screen: B },
C: { screen: C },
D: { screen: D },
}
}, {
contentComponent: DashboardDrawerComponent,
drawerWidth: 280
});
Run Code Online (Sandbox Code Playgroud)
我的堆栈中有 4 个屏幕 - A、B、C、D。我想从 D 跳到 A。(或 D 跳到任何屏幕)我参考了以下 react-navigation 文档- https://reactnavigation.org/docs/navigators/navigation-prop#goBack-Close-the-active-屏幕后移
上面的文档。指出要从屏幕 D 转到屏幕 A(弹出 D、C 和 B),您需要提供一个键来返回从,在我的情况下为 B,如下所示
navigation.goBack(SCREEN_KEY_B)
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是我应该从哪里获得特定屏幕的密钥? 我检查了我的根导航对象,它向我显示了每个屏幕的一些动态生成的键。如何为屏幕指定我自己的键?
这很棘手!
我参考了react-navigation文档的这一段,并且已经实现了上面的功能! https://reactnavigation.org/docs/routers/#Custom-Navigation-Actions
就是这样,
1.稍微改变了问题中我的DrawerNavigator(以适应以下stacknavigator)
const DrawerStackNavigator = new StackNavigator({
A: { screen: A },
B: { screen: B },
C: { screen: C },
D: { screen: D },
}
});
const DashboardDrawer = DrawerNavigator({
DashboardScreen: DrawerStackNavigator,
}, {
contentComponent: DashboardDrawerComponent,
drawerWidth: 280
});
Run Code Online (Sandbox Code Playgroud)
2.在屏幕 D 中调度一个动作
const {navigation} = this.props;
navigation.dispatch({
routeName: 'A',
type: 'GoToRoute',
});
Run Code Online (Sandbox Code Playgroud)
3.在我的堆栈导航器上听这个动作
const defaultGetStateForAction = DrawerStackNavigator.router.getStateForAction;
DrawerStackNavigator.router.getStateForAction = (action, state) => {
if (state && action.type === 'GoToRoute') {
let index = state.routes.findIndex((item) => {
return item.routeName === action.routeName
});
const routes = state.routes.slice(0, index+1);
return {
routes,
index
};
}
return defaultGetStateForAction(action, state);
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6388 次 |
| 最近记录: |