在 React Native 中使用 react-navigation 创建带有道具的导航按钮
Create a navigation button with a props using react-navigation in React Native
我正在尝试创建一个可以帮助我在堆栈之间导航的组件 Button。我使用 react-navigation 在应用程序中导航。问题是我有 3 个文件:
-App.js
<SafeAreaProvider>
<StatusBar barStyle="dark-content" backgroundColor="#ecf0f1" />
<NavigationContainer>
<Stack.Navigator initialRouteName="Accueil">
<Stack.Screen name="Accueil" component={AccueilScreen} options={{headerShown: false}}/>
<Stack.Screen name="Progression" component={ProgressionScreen} />
<Stack.Screen name="Themes" component={ThemesScreen} />
<Stack.Screen name="Quizz" component={QuizzScreen} />
</Stack.Navigator>
</NavigationContainer>
</SafeAreaProvider>
-Accueil.js
function Accueil({ navigation }) {
return (
<SafeAreaView style={styles.container}>
<View style={styles.content_container}>
<Image style={styles.logo} source={require('../assets/logo.jpg')} />
<Button buttonText="Progression" navigation={navigation}/>
</View>
</SafeAreaView>
)
}
-Button.js
export default function Button(props, navigation) {
return (
<TouchableOpacity style={styles.button} onPress={() => navigation.navigate(props.buttonText)}>
<Text style={styles.button_text}>{props.buttonText}</Text>
</TouchableOpacity>
)
}
我的问题是我无法使 navigation.navigate 在 Button 组件中工作,而当我将它直接放在 Accueil.js 文件中时它完全可以工作。
你能帮帮我吗?
navigation
是 props
的一部分,来自您的 Button
组件
export default function Button({ navigation, ...props }) {
return (
<TouchableOpacity style={styles.button} onPress={() => navigation.navigate(props.buttonText)}>
<Text style={styles.button_text}>{props.buttonText}</Text>
</TouchableOpacity>
)
}
你也可以这样做:
export default function Button(props) {
const { navigation } = props
...rest of your code
我正在尝试创建一个可以帮助我在堆栈之间导航的组件 Button。我使用 react-navigation 在应用程序中导航。问题是我有 3 个文件:
-App.js
<SafeAreaProvider>
<StatusBar barStyle="dark-content" backgroundColor="#ecf0f1" />
<NavigationContainer>
<Stack.Navigator initialRouteName="Accueil">
<Stack.Screen name="Accueil" component={AccueilScreen} options={{headerShown: false}}/>
<Stack.Screen name="Progression" component={ProgressionScreen} />
<Stack.Screen name="Themes" component={ThemesScreen} />
<Stack.Screen name="Quizz" component={QuizzScreen} />
</Stack.Navigator>
</NavigationContainer>
</SafeAreaProvider>
-Accueil.js
function Accueil({ navigation }) {
return (
<SafeAreaView style={styles.container}>
<View style={styles.content_container}>
<Image style={styles.logo} source={require('../assets/logo.jpg')} />
<Button buttonText="Progression" navigation={navigation}/>
</View>
</SafeAreaView>
)
}
-Button.js
export default function Button(props, navigation) {
return (
<TouchableOpacity style={styles.button} onPress={() => navigation.navigate(props.buttonText)}>
<Text style={styles.button_text}>{props.buttonText}</Text>
</TouchableOpacity>
)
}
我的问题是我无法使 navigation.navigate 在 Button 组件中工作,而当我将它直接放在 Accueil.js 文件中时它完全可以工作。
你能帮帮我吗?
navigation
是 props
的一部分,来自您的 Button
组件
export default function Button({ navigation, ...props }) {
return (
<TouchableOpacity style={styles.button} onPress={() => navigation.navigate(props.buttonText)}>
<Text style={styles.button_text}>{props.buttonText}</Text>
</TouchableOpacity>
)
}
你也可以这样做:
export default function Button(props) {
const { navigation } = props
...rest of your code