reactjs - React 导航共享元素 5 嵌套导航器

标签 reactjs react-native react-navigation-v5 shared-element-transition

我在使用react-navigation-shared-element 5时遇到了一个小问题,所以我制作了一个非常基本的应用程序来展示一个示例。基本上,我有一个嵌套在堆栈导航器中的选项卡导航器,当导航到选项卡导航器时,我希望进行共享元素转换。这是我到目前为止所拥有的。当我在 Stack Navigator 中同时拥有 MainScreen 和 OtherScreen 时,转换正常,但是当我将 OtherScreen 移动到选项卡导航器时,它停止了。

import {NavigationContainer} from '@react-navigation/native';
import MainScreen from './src/MainScreen';
import OtherScreen from './src/OtherScreen';
import OtherScreenSecond from './src/OtherScreenSecond';
import {createSharedElementStackNavigator} from 'react-navigation-shared-element';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';

const Stack = createSharedElementStackNavigator();
const Tab = createBottomTabNavigator();

const TabStuff = () => {
  return (
    <Tab.Navigator>
      <Tab.Screen name="OtherScreen" component={OtherScreen} />
      <Tab.Screen name="OtherScreenSecond" component={OtherScreenSecond} />
    </Tab.Navigator>
  );
};

const App = () => {
  return (
    <NavigationContainer>
      <Stack.Navigator mode="modal" headerMode="none">
        <Stack.Screen name="MainScreen" component={MainScreen} />
        <Stack.Screen name="Tabs" component={TabStuff} />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

export default App;
import {View, Text, StyleSheet, TouchableOpacity} from 'react-native';
import {SharedElement} from 'react-navigation-shared-element';

const MainScreen = (props) => {
  return (
    <View style={{...styles.container}}>
      <SharedElement id="test">
        <View
          style={{
            width: 100,
            height: 100,
            justifyContent: 'center',
            alignItems: 'center',
            borderRadius: 50,
            backgroundColor: 'orange',
            marginBottom: 50,
          }}></View>
      </SharedElement>
      <TouchableOpacity onPress={() => props.navigation.navigate('Tabs')}>
        <Text>Next</Text>
      </TouchableOpacity>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'red',
  },
});

export default MainScreen;
import {View, Text, StyleSheet, TouchableOpacity} from 'react-native';
import {SharedElement} from 'react-navigation-shared-element';

const OtherScreen = (props) => {
  return (
    <View style={{...styles.container}}>
      <SharedElement id="test">
        <View
          style={{
            width: 150,
            height: 150,
            justifyContent: 'center',
            alignItems: 'center',
            borderRadius: 75,
            backgroundColor: 'blue',
            marginBottom: 50,
          }}></View>
      </SharedElement>
      <TouchableOpacity onPress={() => props.navigation.pop()}>
        <Text>Back</Text>
      </TouchableOpacity>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'yellow',
  },
});

OtherScreen.sharedElements = (route, otherRoute, showing) => {
  return ['test'];
};

export default OtherScreen;

任何指示将不胜感激,谢谢!

最佳答案

您必须将 sharedElementsConfig 添加到 Stack。指定返回导航时要工作的共享元素 id 的屏幕

将代码更改为:

const App = () => {
  return (
    <NavigationContainer>
      <Stack.Navigator mode="modal" headerMode="none">
        <Stack.Screen 
          name="MainScreen" 
          sharedElementsConfig={(props) => [
            {
              id: 'test', animation: 'fade' 
            }
          ]} 
          component={MainScreen} />
        <Stack.Screen name="Tabs" component={TabStuff} />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

关于reactjs - React 导航共享元素 5 嵌套导航器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65193464/

相关文章:

javascript - 将事件类添加到 react 中的按钮

android - ./gradlew assembleRelease 更新以将 native 59.8 和 gradle react 为 5.1 时构建失败

reactjs - react native : Under what circumstances do you have to manually add dependencies of your dependencies?

react-navigation-stack - react 导航 5 : what should be the back-button behaviour with deep nested stack navigators

reactjs - 如何 Jest 模拟 AWS 库

reactjs - 如何在 React 中使用 Swiper 回调方法?

javascript - 子菜单不适用于 ReactJs 中的多个项目?

react-native - 我无法从 react-native 安装 apk 电话我 ""未安装应用程序""اا

react-native - tabBarOnPress 在 React Navigation v5 中不可用

reactjs - 是否可以使用带有React-Navigation(v5)的UIModalPresentationPageSheet或UIModalPresentationFormSheet样式来呈现模态?