Hello I try to pass the name of the city from a component to a TabNavigator to use it in all the screens of the TabNavigator.
App:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { MaterialCommunityIcons, MaterialIcons, FontAwesome, Entypo, FontAwesome5, Ionicons, AntDesign } from '@expo/vector-icons';
import Home from './components/home';
import Localisation from './components/OAD/localisation';
import Exploitation from './components/OAD/monExploit';
import RisquesRDT from './components/OAD/risquesRDT';
import RisquesPRIX from './components/OAD/risquesPRIX';
import Resistance from './components/OAD/resistance';
import Optimisation from './components/OAD/optimisation';
import Sol from './components/SiteWeb/solutions';
import Abo from './components/SiteWeb/abonnements';
import About from './components/SiteWeb/QuiSommesNous';
import Join from './components/SiteWeb/nousRejoindre';
import Contact from './components/SiteWeb/nousContacter';
const Tab = createBottomTabNavigator();
function SiteTabs( { navigation } ) {
return (
<Tab.Navigator initialRouteName="Solutions"
screenOptions={({
tabBarActiveTintColor: '#8197F8',
tabBarInactiveTintColor: 'gray',
headerRight: () => (
<Entypo name="home" size={20} color="#8197F8" onPress={() => navigation.navigate('Home')} style={styles.HOME} />
),
headerTitleAlign: "center",
headerStyle : {backgroundColor:'#E2ECF1'},
tabBarStyle: { backgroundColor:'#E2ECF1'},
})}>
<Tab.Screen
name="Solutions"
component={Sol}
options={{
tabBarLabel: 'Solutions',
tabBarIcon: ({ color, size }) => (
<AntDesign name="check" color={color} size={size} />
),
}}/>
<Tab.Screen
name="Abonnements"
component={Abo}
options={{
tabBarLabel: 'Abonnements',
tabBarIcon: ({ color, size }) => (
<AntDesign name="solution1" color={color} size={size} />
),
}}/>
<Tab.Screen
name="Qui sommes-nous ?"
component={About}
options={{
tabBarLabel: "A propos",
tabBarIcon: ({ color, size }) => (
<AntDesign name="questioncircleo" size={size} color={color} />
),
}}/>
<Tab.Screen
name="Nous rejoindre"
component={Join}
options={{
tabBarLabel: 'Nous rejoindre',
tabBarIcon: ({ color, size }) => (
<FontAwesome5 name="handshake" color={color} size={size} />
),
}}/>
<Tab.Screen
name="Nous contacter"
component={Contact}
options={{
tabBarLabel: 'Contact',
tabBarIcon: ({ color, size }) => (
<AntDesign name="phone" color={color} size={size} />
),
}}/>
</Tab.Navigator>
);
}
function OADTabs( { navigation } ) {
return (
<Tab.Navigator initialRouteName="Mon exploitation"
screenOptions={({
tabBarActiveTintColor: '#91CF4E',
tabBarInactiveTintColor: 'gray',
headerRight: () => (
<Entypo name="home" size={20} color="#91CF4E" onPress={() => navigation.navigate('Home')} style={styles.HOME} />
),
headerTitleAlign: "center",
headerStyle : {backgroundColor:'#E2ECF1'},
tabBarStyle: { backgroundColor:'#E2ECF1'},
})}>
<Tab.Screen
name="Mon exploitation"
component={Exploitation}
options={{
tabBarLabel: 'Exploitation',
tabBarIcon: ({ color, size }) => (
<MaterialIcons name="agriculture" color={color} size={size} />
),
}}/>
<Tab.Screen
name="Risques rendement"
component={RisquesRDT}
options={{
tabBarLabel: 'Risques rdt',
tabBarIcon: ({ color, size }) => (
<Ionicons name="thunderstorm" color={color} size={size} />
),
}}/>
<Tab.Screen
name="Risques prix"
component={RisquesPRIX}
options={{
tabBarLabel: 'Risques prix',
tabBarIcon: ({ color, size }) => (
<FontAwesome name="euro"color={color} size={size} />
),
}}/>
<Tab.Screen
name="Résistance"
component={Resistance}
options={{
tabBarLabel: 'Résistance',
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="arm-flex" size={size} color={color} />
),
}}/>
<Tab.Screen
name="Optimisation"
component={Optimisation}
options={{
tabBarLabel: 'Optimisation',
tabBarIcon: ({ color, size }) => (
<FontAwesome5 name="piggy-bank" color={color} size={size} />
),
}}/>
</Tab.Navigator>
);
}
const Stack = createNativeStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator screenOptions={{contentStyle:{backgroundColor:'#FFFFFF'}, headerTitleAlign: "center",}} initialRouteName="Home">
<Stack.Screen
name="Home"
component={Home}
options={{headerShown: false}} />
<Stack.Screen
name="Localisation"
component={Localisation} />
<Stack.Screen
name="Ma simulation"
component={OADTabs}
options={{headerShown: false}} />
<Stack.Screen
name="Solutions"
component={SiteTabs}
options={{headerShown: false}} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
const styles = StyleSheet.create({
HOME: {
marginRight: 20,
}
})
Localisation :
import React, {useState} from 'react';
import { Text, View, StyleSheet, TextInput, TouchableOpacity } from 'react-native';
import RNPickerSelect from 'react-native-picker-select'
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import communes from "../data/communes"
export default function Localisation({ navigation }) {
const [city, setCity]= React.useState('');
return (
<View style={styles.plus}>
<Text> Selectionner la commune de votre choix </Text>
<RNPickerSelect
style={pickerSelectStyles}
onValueChange={(itemValue, itemIndex) => setCity(itemValue)}
placeholder={{label: 'Choisissez une ville', value: null, color: '#9EA0A4',}}
items={[
{label:"78850 - Thiverval-Grignon", value:"Grignon"},
{label:"A", value:"PdT oise"},
{label:"B", value:"Bett Oise"},
{label:"C", value:"Grand Est"},
{label:"D", value:"Berry"},
{label:"E", value:"Charrente"},
{label:"F", value:"Sud Ouest"},]}/>
<Text> {city} </Text>
<View style={{alignItems: "center",}}>
<TouchableOpacity onPress={() => { navigation.navigate('Ma simulation', {ville: city} ) }} style={styles.button} >
<Text> Valider </Text>
</TouchableOpacity>
</View>
</View>
);
}
I tried
- to passe it in the Tab.Navigator as a screenProps
- to pass it like a route.params
- I read the documentation in react navigation to pass params in an other screen and also the docs of the TabNavigation and try their codes but it doens’t work.
But nothing works in the App fiel the const ville is any however its “string” in the localisation fiel.
Someone can help me ?