Is it possible to pass params from a React component or React Native screen to a react native screen navigator within a stack navigation.
The reason I would like to achieve this is the screen navigator has a button in it that needs to be passed a function.
So to be clear, I am using useState in a screen to save some values I am getting from a form. I would like to pass these values to the stack navigator so they can be used in a function that makes a call to an api.
Please note this question is not about passing params from screen to screen with React Navigation, I know how to achieve this using route params.
CodePudding user response:
App.js
import 'react-native-gesture-handler';
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import FirstPage from './pages/FirstPage';
import SecondPage from './pages/SecondPage';
const Stack = createStackNavigator();
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="FirstPage">
<Stack.Screen
name="FirstPage"
component={FirstPage}
options={{
title: 'First Page', //Set Header Title
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
headerTintColor: '#fff', //Set Header text color
headerTitleStyle: {
fontWeight: 'bold', //Set Header text style
},
}}
/>
<Stack.Screen
name="SecondPage"
component={SecondPage}
options={{
title: 'Second Page', //Set Header Title
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
headerTintColor: '#fff', //Set Header text color
headerTitleStyle: {
fontWeight: 'bold', //Set Header text style
},
}}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
FristPage.js
import React, {useState} from 'react';
import {
SafeAreaView,
StyleSheet,
View,
Text,
TextInput,
Button,
} from 'react-native';
const FirstPage = ({navigation}) => {
const [userName, setUserName] = useState('AboutReact');
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<Text style={styles.heading}>
React Native Pass Value From One Screen to Another Using React
Navigation
</Text>
<Text style={styles.textStyle}>
Please insert your name to pass it to second screen
</Text>
{/*Input to get the value from the user*/}
<TextInput
value={userName}
onChangeText={(username) => setUserName(username)}
placeholder={'Enter Any value'}
style={styles.inputStyle}
/>
{/* On click of the button we will send the data as a Json
From here to the Second Screen using navigation */}
<Button
title="Go Next"
//Button Title
onPress={() =>
navigation.navigate('SecondPage', {
paramKey: userName,
})
}
/>
</View>
<Text style={{textAlign: 'center', color: 'grey'}}>
www.aboutreact.com
</Text>
</SafeAreaView>
);
};
export default FirstPage;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
padding: 20,
},
heading: {
fontSize: 25,
textAlign: 'center',
marginVertical: 10,
},
textStyle: {
textAlign: 'center',
fontSize: 16,
marginVertical: 10,
},
inputStyle: {
width: '80%',
height: 44,
padding: 10,
marginVertical: 10,
backgroundColor: '#DBDBD6',
},
});
secondPage.js
import React from 'react';
import {SafeAreaView, StyleSheet, View, Text} from 'react-native';
const SecondPage = ({route}) => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<Text style={styles.heading}>
React Native Pass Value From One Screen to Another Using React
Navigation
</Text>
<Text style={styles.textStyle}>
Values passed from First page: {route.params.paramKey}
</Text>
</View>
<Text style={{textAlign: 'center', color: 'grey'}}>
www.aboutreact.com
</Text>
</SafeAreaView>
);
};
export default SecondPage;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
padding: 20,
},
heading: {
fontSize: 25,
textAlign: 'center',
marginVertical: 10,
},
textStyle: {
textAlign: 'center',
fontSize: 16,
marginVertical: 10,
},
});
here is more detalis
Hope this will help you!!
CodePudding user response:
I suggest that if you hide the stack header & create your own header in the screen component, you can get all the parameters from params
CodePudding user response:
Yes, it is possible to pass parameters from a React component or React Native screen to a screen navigator within a stack navigation.
One way to achieve this is to use the navigation prop that is passed to every screen component by the createStackNavigator function. You can pass parameters to the screen navigator by using the navigate function of the navigation prop and passing an object containing the parameters as the second argument.
For example, suppose you have a form in a screen component and you want to pass the values from the form to the screen navigator.
//You can use the navigation prop in the form submission handler to pass the values to the screen navigator like this:
handleSubmit = values => {
this.props.navigation.navigate('ScreenNavigator', { values });
};
//In the screen navigator, you can access the values parameter like this:
const values = this.props.navigation.getParam('values');
//You can then use the values parameter in a function that makes a call to an API.
I hope this helps!