react-navigation - 如何为特定路线指定自定义转换,而不是屏幕(v5)
问题描述
关于反应导航 v5 的问题。
在以前的版本中,我们可以通过在 a 中执行以下操作来为特定路由指定自定义转换,而不是屏幕StackNavigator
:
transitionConfig: () => ({
screenInterpolator: sceneProps => {
const {scenes, scene} = sceneProps;
const prevRoute = scenes[0].route.routeName === 'Route A';
// If prev route is A, and then current route is B, then we do a specific transition
if (prevRoute && scene.route.routeName === 'Route B') {
return StackViewStyleInterpolator.forVertical(sceneProps);
}
// Otherwise default to normal transition
return StackViewStyleInterpolator.forHorizontal(sceneProps);
},
}),
现在,我正在尝试为 react-navigation v5 实现相同的目标。我知道我可以通过执行以下操作指定每个屏幕的自定义动画:
<Stack.Screen name="Route B" component={RouteB} options={{ cardStyleInterpolator: CardStyleInterpolators.forVerticalIOS }} />
问题是我不希望每次导航到 时都应用此转换RouteB
,仅当上一条路线为 时RouteA
,我希望应用此转换,就像上面的前一个代码块一样。
在文档中找不到任何示例,因此希望能在将代码迁移到 v5 方面提供一些帮助。
解决方案
像这样的东西应该工作:
options={({ navigation, route }) => {
const state = navigation.dangerouslyGetState();
const index = state.routes.indexOf(route);
const previousRoute = state.routes[index - 1];
if (previousRoute?.name === 'RouteA') {
return {
cardStyleInterpolator: CardStyleInterpolators.forVerticalIOS,
gestureDirection: 'vertical',
};
} else {
return {};
}
}}
推荐阅读
- javascript - Vue.js - 通过另一个 JSON 数组过滤 JSON 数组
- react-native - undefined 不是评估“_usecontext.user”的对象
- python - 匹配最外层大括号python的内容
- css - 离子按钮单击以更改网格的下一列或下一行的颜色
- openssl - 如何使用 Cygwin 使 OpenSSL 与 socat 一起工作?
- javascript - 当元素在视口中使用vanilla javascript突出显示导航栏列表?
- symfony - LDAP 和 Symfony:无法使用 dn 完成搜索
- sql - 计算持续时间 SQL 的总和
- anylogic - 从文件夹资源中的其他路径删除数据
- azure-devops - 需要在规划下的 Azure DevOps 中添加自定义值