I'm creating an app with React Native and i need to send params from one screen to all of the screens of a tab Navigator.
Screen summary:
- Login
- Home
- Profile (Tab)
- Settings (Tab)
I need to send the name of the user from Login to Home and from Home to Profile and Settings
App.js
import 'react-native-gesture-handler';
import * as React from 'react';
import Icon from 'react-native-vector-icons/FontAwesome';
import { createStackNavigator } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import Settings from './Screens/Settings';
import Profile from './Screens/Profile';
import Home from './Screens/Home';
import Login from './Screens/Login';
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();
function TabRoutes() {
return (
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName;
if (route.name === 'Profile') {
iconName = focused ? 'home' : 'home';
} else if (route.name === 'Settings') {
iconName = focused ? 'cog' : 'cog';
}
return <Icon name={iconName} size={size} color={color} />;
},
})}
tabBarOptions={{
activeTintColor: 'tomato',
inactiveTintColor: 'gray',
}}
>
<Tab.Screen name="Profile" component={Profile} />
<Tab.Screen name="Settings" component={Settings} />
</Tab.Navigator>
);
}
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Login" component={Login} />
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Tabs" component={TabRoutes} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
Login.js
export default class Login extends React.Component {
render() {
return (
<View>
<Text>Welcome</Text>
<Button title="Log in" onPress={() => this.props.navigation.navigate('Home', {name: 'Sergio'})}/>
</View>
)
}
}
Home.js -> Here i need to send to all the tabs the name of the user
export default class Home extends React.Component {
render() {
const { name} = this.props.route.params;
return (
<View>
<Text>Home</Text>
<Text>Hello {name}</Text>
<Button title="GO TO TABS" onPress={() => this.props.navigation.navigate('Tabs')}/>
</View>
)
}
}
Profile.js
export default class Profile extends React.Component {
render() {
return (
<View>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
/>
<Text>Profile</Text>
<Text>Hello @</Text> //Here i want to see the name of the user
</View>
);
}
}
How am I sending and receiving the parameters
From Login to Home:
Login.js:
<Button title="Log in" onPress={() => this.props.navigation.navigate('Home', {name: 'Sergio'})}/>
Home.js:
const { name} = this.props.route.params;
console.log(name); //The result is Sergio
From Home to Profile/Settings:
Home.js:
<Button title="GO TO TABS" onPress={() => this.props.navigation.navigate('Tabs', {name})}/>
Profile.js:
v1 = const { name} = this.props.navigation.state.params.name
v2 = const { name} = this.props.route.params;
console.log(name); //In both the value is undefined