如何使用反应导航从标题中的按钮“导航”到新屏幕?

nbu*_*urk 1 javascript reactjs react-navigation

我正在使用react-navigation并且要在标题的右侧添加一个按钮,该按钮将导航到其他屏幕。但是在设置时navigationOptions,我想知道如何navigate在按下按钮时调用的回调函数中访问该函数:

const RootNavigationStack = StackNavigator({
  PokemonList: {
    screen: PokemonListWrapper,
    navigationOptions: {
      title: ({state}) => `Pokedex`,
      header: ({ state, setParams }) => {
        return {
          visible: true,
          right: (
            <Button
              title={'Add'}
              onPress={() => // navigate to new screen here
            />
          ),
        }
      }
    },
  },
, {
  headerMode: 'screen'
})
Run Code Online (Sandbox Code Playgroud)

有什么想法可以实现吗?我当时正在考虑使用withNavigation,但仍不清楚如何使用。

Uma*_*wan 5

我使用这个解决了相同的问题(在Screen / Component Class内部)

static navigationOptions = ({navigation}) => {
  return {
    title: 'Next',
    headerRight: <Button
                     title="Next"

                     onPress={ () => navigation.navigate('RegPasswordScreen')} />

  };
};
Run Code Online (Sandbox Code Playgroud)


San*_*gar 5

另一个答案适用于旧版本的React Navigation。

对于新版本,请尝试以下操作:

static navigationOptions = ({ navigation }) => ({
        headerRight: (
            <Button 
                title='Setting' 
                onPress={ () => navigation.navigate('RouteName') }                
                backgroundColor= "rgba(0,0,0,0)"
                color="rgba(0,122,255,1)"
            />
        )});
Run Code Online (Sandbox Code Playgroud)