React native 如何在React Native中使用stackNavigation更改屏幕?

React native 如何在React Native中使用stackNavigation更改屏幕?,react-native,react-navigation,React Native,React Navigation,我有一个屏幕,在标题中包含一个按钮,用于转到另一个屏幕 我已经在这里建立了模型,但它不起作用:如下所示,我想使用页眉中的按钮将屏幕从RecipeList更改为NewRecipeForm const AppStackNavigator = createStackNavigator({ List: { screen: RecipesList, navigationOptions: { title:'RecipesList', headerLeft: ( <

我有一个屏幕,在标题中包含一个按钮,用于转到另一个屏幕

我已经在这里建立了模型,但它不起作用:如下所示,我想使用页眉中的按钮将屏幕从RecipeList更改为NewRecipeForm

const AppStackNavigator = createStackNavigator({
 List: {
  screen: RecipesList,
  navigationOptions: {
    title:'RecipesList',
    headerLeft: (
      <Button onPress={()=>this.props.navigation.navigate('NewRecipeForm')}>
      <Text>+</Text>
      </Button>
    )
    }},
 NewRecipeForm: {screen: CreateRecipeForm, 
        navigationOptions: {title:'Add new recipe'}},
  Details: {screen: RecipesDetails, navigationOptions: {title:'RecipeDetails'}},

export default class App extends React.Component {
 render() {
  return <AppStackNavigator initialRouteName='List' />;
   }
}
const AppStackNavigator=createStackNavigator({
名单:{
屏幕:菜谱列表,
导航选项:{
标题:“配方列表”,
左校长:(
this.props.navigation.navigate('NewRecipeForm')}>
+
)
}},
NewRecipeForm:{屏幕:CreateRecipeForm,
导航选项:{title:'addnewrecipe'}},
详细信息:{屏幕:配方详细信息,导航选项:{标题:'RecipedDetails'},
导出默认类App扩展React.Component{
render(){
返回;
}
}
我希望你能帮我解决这个问题


您无法在headerLeft中访问组件的道具,但可以直接使用如下导航:

  <Button onPress={()=> navigation.navigate('NewRecipeForm')}>
navigation.navigate('NewRecipeForm')}>

您无法在headerLeft中访问组件的道具,但您可以直接使用如下导航:

  <Button onPress={()=> navigation.navigate('NewRecipeForm')}>
navigation.navigate('NewRecipeForm')}>

您可以在RecipesList组件中使用以下代码,而不是将其放在createStackNavigator()中。有关完整实现,请参见此

static navigationOptions = ({ navigation }) => {
    return {
      headerTitle: "RecipesList",
      headerLeft: (
        <Button
          onPress={() => navigation.navigate('NewRecipeForm')}
          title="+"
        />
      ),
    };
  };
静态导航选项=({navigation})=>{
返回{
标题:“食谱列表”,
左校长:(
navigation.navigate('NewRecipeForm')}
title=“+”
/>
),
};
};

您可以在RecipesList组件中使用以下代码,而不是将其放在createStackNavigator()中。有关完整实现,请参见此

static navigationOptions = ({ navigation }) => {
    return {
      headerTitle: "RecipesList",
      headerLeft: (
        <Button
          onPress={() => navigation.navigate('NewRecipeForm')}
          title="+"
        />
      ),
    };
  };
静态导航选项=({navigation})=>{
返回{
标题:“食谱列表”,
左校长:(
navigation.navigate('NewRecipeForm')}
title=“+”
/>
),
};
};

您可以使用堆栈导航器,如下所示,您可以在createStackNavigator中提供navigationOptions属性的同时对导航属性进行分解

const AppStackNavigator = createStackNavigator({
    List: {
        screen: RecipesList,
        navigationOptions: ({navigation}) => {  //destructure the navigation property here 
            return {
                title: 'RecipesList',
                headerLeft: (
                    <Button onPress={() => navigation.navigate('NewRecipeForm')}>
                        <Text>+</Text>
                    </Button>
                )
            }
        }
    },
    NewRecipeForm: {
        screen: CreateRecipeForm,
        navigationOptions: { title: 'Add new recipe' }
    },
    Details: { screen: RecipesDetails, navigationOptions: { title: 'RecipeDetails' } }

});
const AppStackNavigator=createStackNavigator({
名单:{
屏幕:菜谱列表,
navigationOptions:({navigation})=>{//在此处解构导航属性
返回{
标题:“配方列表”,
左校长:(
navigation.navigate('NewRecipeForm')}>
+
)
}
}
},
新RecipeForm:{
屏幕:CreateRecipeForm,
导航选项:{title:'addnewrecipe'}
},
详细信息:{屏幕:配方详细信息,导航选项:{标题:'RecipedDetails'}
});

您可以使用堆栈导航器,如下所示,您可以在createStackNavigator中提供navigationOptions属性的同时对导航属性进行分解

const AppStackNavigator = createStackNavigator({
    List: {
        screen: RecipesList,
        navigationOptions: ({navigation}) => {  //destructure the navigation property here 
            return {
                title: 'RecipesList',
                headerLeft: (
                    <Button onPress={() => navigation.navigate('NewRecipeForm')}>
                        <Text>+</Text>
                    </Button>
                )
            }
        }
    },
    NewRecipeForm: {
        screen: CreateRecipeForm,
        navigationOptions: { title: 'Add new recipe' }
    },
    Details: { screen: RecipesDetails, navigationOptions: { title: 'RecipeDetails' } }

});
const AppStackNavigator=createStackNavigator({
名单:{
屏幕:菜谱列表,
navigationOptions:({navigation})=>{//在此处解构导航属性
返回{
标题:“配方列表”,
左校长:(
navigation.navigate('NewRecipeForm')}>
+
)
}
}
},
新RecipeForm:{
屏幕:CreateRecipeForm,
导航选项:{title:'addnewrecipe'}
},
详细信息:{屏幕:配方详细信息,导航选项:{标题:'RecipedDetails'}
});