Can’t find variable: navigation error when trying to navigate inside from inside the stack navigator

╄→尐↘猪︶ㄣ 提交于 2021-02-10 15:06:13

问题


I've added a header button and I'd like to go to a specific screen in my app when I click on that button, here's the code:

function ActionBarIcon(props) {
  return (
    <TouchableOpacity onPress={props.onPress}>
      <Image
        source={{uri : 'https://static.thenounproject.com/png/261370-200.png'}}
        style={{ width: 30, height: 30, marginRight : 15 }} />
    </TouchableOpacity>
  );
}

export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="Orders">

        <Stack.Screen name="Orders" component={HomePage} options = {{
          headerRight : props => <ActionBarIcon {...props}
          onPress={() => {
            navigation.navigate('Create Order')
          }} />
        }}/>

        <Stack.Screen name="Order Details" component={DetailsPage} />
        <Stack.Screen name="Create Order" component={CreateOrderPage} />

      </Stack.Navigator>
    </NavigationContainer>
  );
}

As you can see, I want to go to create order page, when the header button is clicked but I'm getting an error saying "Can’t find variable: navigation". I appreciate your feedback!


回答1:


It must not be getting navigation inside headerRight, so you may try the below suggestion

 <Stack.Screen name="Orders" component={HomePage} options={({ navigation, route }) => ({
      headerRight: props => <ActionBarIcon {...props}
          onPress={() => {
            navigation.navigate('Create Order') 
          }} />
    })}

    />


来源:https://stackoverflow.com/questions/62714514/can-t-find-variable-navigation-error-when-trying-to-navigate-inside-from-inside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!