title: React-Native: How to add an icon on the top bar that makes the popToTop?
body: <p>I would like to add a button in the topBar that does popToTop only in two screens of the navigation stack. I tried to do this, but apparently the navigation does not work.</p>
  <p>My code is as follow:</p>
  <pre><code>const Stack = createStackNavigator();
  
  export default function App({navigation}) {
  
    return (
      &lt;NavigationContainer&gt;
  
        &lt;Stack.Navigator&gt;
  
          &lt;Stack.Screen
            name='MyMusic'
            component={TabNavigator}
          /&gt;
  
          &lt;Stack.Screen
            name='Search song'
            component={SongScreen}
            options={{
              headerRight: () =&gt; (
                &lt;AntDesign
                  name='home'
                  color='white'
                  size={25}
                  style={{justifyContent: 'center', padding: 15}}
                  onPress={() =&gt; navigation.dispatch(StackActions.popToTop())}
                /&gt;
              ),
            }}
          /&gt;
  
          &lt;Stack.Screen
            name='Search artist'
            component={ArtistScreen}
            options={{
              headerRight: () =&gt; (
                &lt;AntDesign
                  name='home'
                  color='white'
                  size={25}
                  style={{justifyContent: 'center', padding: 15}}
                  onPress={() =&gt; navigation.dispatch(StackActions.popToTop())}
                /&gt;
              ),
            }}
          /&gt;
  
        &lt;/Stack.Navigator&gt;
      &lt;/NavigationContainer&gt;
    );
  }
  </code></pre>
  <p>But trying on the emulator this error occurs, after clicking:</p>
  <pre><code> TypeError: undefined is not an object (evaluating 'navigation.dispatch')
  </code></pre>
  <p><strong>EDIT</strong>: <code>App.js</code>'s code. I marked with the comment 'not work' the interested line:</p>
  <pre><code>import React from 'react';
  import { StatusBar } from 'react-native';
  import { NavigationContainer, useNavigation } from '@react-navigation/native';
  import { createStackNavigator } from '@react-navigation/stack';
  import TabNavigator from './components/navigation/TabNavigator';
  import SongScreen from './components/screens/SongScreen';
  import ArtistScreen from './components/screens/ArtistScreen';
  import AntDesign from 'react-native-vector-icons/AntDesign';
  
  
  const Stack = createStackNavigator();
  const colorStatusBar = Platform.OS === 'android' ? '#4527A0' : '#673AB7';
  
  export default function App() {
  
    const navigation = useNavigation();
  
    return (
      &lt;NavigationContainer&gt;
        &lt;StatusBar
          backgroundColor={colorStatusBar}
        /&gt;
        &lt;Stack.Navigator&gt;
          &lt;Stack.Screen
            name='MyMusic'
            component={TabNavigator}
            options={{
              title: 'MyMusic',
              headerStyle: {
                backgroundColor: '#673AB7',
              },
              headerTintColor: 'white',
            }}
          /&gt;
          &lt;Stack.Screen
            name='Search song'
            component={SongScreen}
            options={{
              title: 'Song Video &amp; Lyrics',
              headerStyle: {
                backgroundColor: '#673AB7',
              },
              headerTintColor: 'white',
              headerRight: () =&gt; ( 
                &lt;AntDesign
                  name='home'
                  color='white'
                  size={25}
                  style={{ justifyContent: 'center', padding: 15 }}
                  onPress={() =&gt; navigation.popToTop()} //&lt;---not work
                /&gt;
              ),
            }}
          /&gt;
          &lt;Stack.Screen
            name='Search artist'
            component={ArtistScreen}
            options={{
              title: 'Artist Details',
              headerStyle: {
                backgroundColor: '#673AB7',
              },
              headerTintColor: 'white',
              headerRight: () =&gt; ( 
                    &lt;AntDesign
                      name='home'
                      color='white'
                      size={25}
                      style={{ justifyContent: 'center', padding: 15 }}
                      onPress={() =&gt; navigation.popToTop()} //&lt;---not work
                    /&gt;
                  ),
                }}
              /&gt;
            &lt;/Stack.Navigator&gt;
          &lt;/NavigationContainer&gt;
        );
      }
  </code></pre>