简体   繁体   English

找不到路线 object。 您的组件是否在导航器的屏幕内? 反应原生

[英]Couldn't find a route object. Is your component inside a screen in a navigator? React Native

I am developing a multi page app using stack navigators in react native.我正在使用堆栈导航器在本机反应中开发一个多页应用程序。 While navigating to different pages, I may/may not pass data, but in some cases like 'Chat' I do pass the userB data to ChatScreen which is defined inside the StackNavigator , so in order to change the Header of my ChatScreen to userB 's displayName, I created a custom headerTitle component for it and passed as a prop in Stack.Screen .在导航到不同页面时,我可能/可能不会传递数据,但在某些情况下,例如“聊天”,我确实将userB数据传递给在ChatScreen中定义的StackNavigator ,因此为了将我的HeaderChatScreen更改为userB ' s displayName,我为它创建了一个自定义headerTitle组件,并在Stack.Screen中作为 prop 传递。 Below is the code:下面是代码:

<Stack.Navigator>
      <Stack.Screen
        name="Home"
        component={HomeScreen}
        options={{headerTitle: HomeHeader}}
      />
      <Stack.Screen
        name="Chat"
        component={ChatScreen}
        options={{headerTitle: (props) => <ChatHeader {...props} />}} // PROBLEM IS HERE...
      />
      <Stack.Screen
        name="Contacts"
        component={ContactScreen}
        options={{title: 'Select Contact'}}
      />
    </Stack.Navigator>

Now, In someother component, I pass some data to Chat screen:现在,在其他组件中,我将一些数据传递给Chat屏幕:

const ContactItem = ({item}) => {
  const navigator = useNavigation();
  const {phone, name, profile} = item;
  const [userB, setUserB] = useState(null);

  return (
    <TouchableOpacity
      onPress={() => {
        const formattedPhone = reformat(phone);
        getUserDetails(formattedPhone, setUserB);
        if (userB) {
          console.log(userB);
          navigator.navigate('Chat', {
            user: userB,
          });
        } else {
          ToastMessage(`User with phone number ${formattedPhone} does not exist`);
        }
      }}>
    </TouchableOpacity>
  );
};

Now, while navigating to Chat I'm passing userB data to Chat Screen I'm getting the following details in ChatHeader component:现在,在导航到Chat时,我将userB数据传递到Chat Screen,我在ChatHeader组件中获得了以下详细信息:

const ChatHeader = () => {
  const navigator = useNavigation();
  const {params} = useRoute();
  const {user} = params;
  console.log(user);

  return (
    <View style={styles.container}>
      <TouchableOpacity>
        <Image
          source={require('../../../assets/images/user-icon.png')}
          style={styles.image}
        />
      </TouchableOpacity>

      <View style={styles.headerTextContainer}>
        <Text style={styles.headerText}>{user.name}</Text>
      </View>
    </View>
  );
};

Error:错误:

Warning: Cannot update a component (`NativeStackNavigator`) while rendering a different component (`ChatScreen`). To locate the bad setState() call inside `ChatScreen`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render  in ChatScreen (at SceneView.tsx:126).

Error: Couldn't find a route object. Is your component inside a screen in a navigator?  This error is located at:in ChatHeader (at navigation/index.js:47)

As I'm passing the props in Stack.Screen options={{headerTitle: (props) => <ChatHeader {...props} />}} so this ensures that I should be able to inherit the data in ChatHeader which is passed to Chat screen component right?当我在Stack.Screen options={{headerTitle: (props) => <ChatHeader {...props} />}}中传递props时,这样可以确保我应该能够继承ChatHeader中的数据,即传递给Chat屏幕组件对吗? But that is not the case...但事实并非如此...

Can someone help me?有人能帮我吗?

Thank you!谢谢!

React Navigation doc's says : React Navigation 文档

In order to use params in the title, we need to make options prop for the screen a function that returns a configuration object.为了在标题中使用参数,我们需要为屏幕创建一个 function 选项,它返回一个配置 object。

So you have to change所以你必须改变

options={{headerTitle: (props) => <ChatHeader {...props} />}} options={{headerTitle: (props) => <ChatHeader {...props} />}}

to

options={(props) => ({headerTitle: <ChatHeader {...props} />})} options={(props) => ({headerTitle: <ChatHeader {...props} />})}

暂无
暂无

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

相关问题 找不到导航 object。您的组件是否在 NavigationContainer 中 - Couldn't find a navigation object. Is your component inside NavigationContainer React Native Navigator Route对象未定义 - React Native Navigator Route Object is Undefined 在本机反应中找不到导航 object 错误 - couldn't find a navigation object error in react native React路由无法通过传递数据来呈现组件 - React route couldn't render component with passing data 如何正确定义 React Native Navigation 的屏幕? (错误:找不到导航器的任何屏幕。您是否定义了任何屏幕...) - How do I properly define screens for React Native Navigation? (Error: Couldn't find any screens for the navigator. Have you defined any screens...) 找不到屏幕“主页”的“组件”或“儿童”道具。 如果您传递了“未定义”,就会发生这种情况。 react-navigate v5 出错? - Couldn't find a 'component' or 'children' prop for the screen 'Home'. This can happen if you passed 'undefined'. Error with react-navigate v5? 将反应组件(或元素?)放入对象中。 - Putting a react component (or element?) in an object. React Navigation v5:如何在子屏幕内获取父导航器的路由参数 - React Navigation v5: How to get route params of the parent navigator inside the child screen 内部React Native <Navigator/> ,如何引用当前传入的route.name? - Inside React Native <Navigator/>, how do I reference the current route.name being passed in? React Native Navigator - 屏幕始终为空白 - React Native Navigator - screen always blank
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM