how to use tabBarOnPress in tabnavigator react native - react-native

i am stuck in big problem that is i wants onPress event when i clicked on tab.
my code is here:-
static navigationOptions = ({navigation, screenProps}) => {
const params = navigation.state.params || {};
console.log("Params:-",params);
return {
title:Strings.title_dashboard,
headerStyle:{ backgroundColor: Colors.header_blue},
headerTitleStyle:HeaderStyle.titleCenter,
tabBarOnPress: (tab, jumpToIndex) => {
console.log("Tab Click",tab);
jumpToIndex(tab.index);
navigation.state.params.onFocus()
},
headerRight:<TouchableOpacity onPress={()=>Alert.alert(Strings.avanza,Strings.under_imple)}><View><Image source={{uri: "filter_icon"}} style={{height: 18, width: 18,marginRight:10,}} /></View></TouchableOpacity>,
}
}
at here i set the Params like this in componentDidMount:
this.props.navigation.setParams({
handleGrid: this.callServer.bind(this)
})
getting an error here not able to get this click event.
Help me please.
Thank you.

This is my approach. It works for the version 5.x.x of react-navigation:
const BottomTab = createBottomTabNavigator();
const Tabs = props => (
<BottomTab.Navigator
initialRouteName="Foo"
...
<BottomTab.Screen
name="Foo"
component={Foo}
initialParams={props.route.params}
listeners={{
tabPress: e => {
// e.preventDefault(); // Use this to navigate somewhere else
console.log("Foo tab bar button pressed")
},
}}
/>
</BottomTab.Navigator>
);
Read more about listeners.
For version 3.x.x and I hope for the 4th as well please use this one.
let Tabs = createBottomTabNavigator(
{
FooTab: Foo,
},
{
initialRouteName: "FooTab",
defaultNavigationOptions: ({ navigation }) => ({
tabBarOnPress: ({ navigation, defaultHandler }) => {
console.log('onPress:', navigation.state.routeName);
defaultHandler()
},
}),
}
);
For version 2.x.x please use navigationOptions instead of the defaultNavigationOptions.

This is working for me,
static navigationOptions = ({ navigation }) => {
return {
tabBarOnPress: ({previousScene, scene, jumpToIndex}) => {
const { route, index, focused} = scene;
if(focused){
navigation.state.params.scrollToTop()
}
jumpToIndex(0)
}
}
};

I used stack navigator
const Stack = createStackNavigator({
ScreenA: {
screen:ScreenA ,
navigationOptions: () => ({
header: null
}),
},
ScreenB: {
screen:ScreenB ,
navigationOptions: () => ({
header: null
}),
},
});
//Added tabBarOnPress
https://reactnavigation.org/docs/en/stack-actions.html
the popToTop action takes you back to the first screen in the stack, dismissing all the others. It's functionally identical to StackActions.pop({n: currentIndex}).
import { StackActions } from 'react-navigation';
let Tabs = createBottomTabNavigator(
{
FooTab: Foo,
},
{
initialRouteName: "FooTab",
defaultNavigationOptions: ({ navigation }) => ({
tabBarOnPress: ({ navigation, defaultHandler }) => {
// to navigate to the top of stack whenever tab changes
navigation.dispatch(StackActions.popToTop());
defaultHandler();
]},
}),
}
);

Related

React Navigation upgrade v4 to v6 - navigationOptions and listener

Previously with React Navigation v4, I have use route.params.scrollToTop() to pass the function I want to call when tabBarOnPress. The function content is not related to scrollToTop. After upgrading to React Navigation v6, I have changed the code. However, with all the changes, the scrollToTop function only called once. Once I switch to another Tab and go back to ScreenB, the componentDidMount do not trigger again. I would like to call the function every time I tap the tab just like how it respond in v4, no matter which screen I'm switching from.
v4
const RouteConfigs = {
ScreenA:{
screen: ComponentA,
},
ScreenB:{
screen: ComponentB,
navigationOptions: {
tabBarOnPress: (event) => {
const { navigation } = event;
event.defaultHandler();
if (navigation.isFocused() && navigation.state.params && navigation.state.params.scrollToTop) {
navigation.state.params.scrollToTop();
}
},
}
}
}
const Tabs = createBottomTabNavigator(RouteConfigs, TabNavigatorConfig)
Tabs.navigationOptions = ({navigation}) => {
const {routeName} = navigation.state.routes[navigation.state.index]
return{
header: null,
}
}
v6
Navigation
const Tab = createBottomTabNavigator();
<Tab.Navigator>
<Tab.Screen
name="ScreenA"
component={ComponentA}
>
<Tab.Screen
name="ScreenB"
component={ComponentB}
listeners={({ navigation, route, params}) => ({
tabPress: (e) => {
if (navigation.isFocused() && route.params && route.params.scrollToTop) {
route.params.scrollToTop();
}
},
})}
>
</Tab.Navigator>
I did not change anything in ComponentB
...
componentDidMount(){
this.props.navigation.setParams({
scrollToTop: this.scrollToTop,
});
}
scrollToTop = ()=>{
console.log("Press")
this.setState(()=> ({...}))
}
...
I am not sure is it because I did not add the part. However I have no idea how to add it under v6.
Tabs.navigationOptions = ({navigation}) => {
const {routeName} = navigation.state.routes[navigation.state.index]
return{
header: null,
}
}

How to pass a null screen in Tab Navigation in React Navigation 5

In previous versions of react navigation, we could do this
const CustomTabNavigator = createBottomTabNavigator({
FirstTab: {
screen: FirstScreen,
},
AddButton: {
screen: () => null,
navigationOptions: () => ({
tabBarIcon: (<AddButton/>),
tabBarOnPress: () => {}
})
},
SecondTab: {
screen: SecondScreen,
}
}
Trying to replicate this with react-navigation is causing errors as it won't accept null. Anyone know a way around this?
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs'
const BottomTabNavigator = createBottomTabNavigator()
<BottomTabNavigator.Navigator>
<BottomTabNavigator.Screen
name="FirstTab"
component={FirstScreen}
/>
<BottomTabNavigator.Screen
name="Add"
component={null}
options: () => ({
tabBarIcon: (<AddButton/>),
tabBarOnPress: () => {}
})
/>
<BottomTabNavigator.Screen
name="Second Tab"
component={SecondScreen}
/>
</BottomTabNavigator.Navigator>
Try this, I originally tried to pass it in as an inline function but I got a warning as well. This solved my problem.
Example:
const AddButton = () => {
return null
}
<Tab.Screen name="AddButton" component={AddButton}/>

Set title in createStackNavigator onPress

I am making react native app. I have button and onPress i want to send headerTitle to createStackNavigator but i have some errors. I am sending title with setParams and in createStackNavigator i get it with getParam.
Why it is not working how is correct?
Code:
// page.js
<TouchableOpacity
onPress={() => navigate('Page2', {
this.props.navigation.setParams({Title: 'Title'});
})}>
// createStackNavigator
export default createStackNavigator(
{
Main: {
screen: Page1,
},
Page2: {
screen: Page2,
navigationOptions: ({ navigation }) => ({
headerTitle: navigation.state.getParam('Title', 'Default Title'),
}),
},
},
{
initialRouteName: 'Main',
}
);
The problem appears to be occurring in the operating order of the app.
Because the value is already obtained before the value is set, the value must be null or undefind, which is the value before the setting.
Try using it as a global function.
globalScreen
let TITLE = "defaultTitle"
function setT(data){
TITLE = data;
}
function getT() {
return TITLE;
}
export { setT, getT }
Usage
// page.js
import {setT } from "globalScreen"
<TouchableOpacity
onPress={() => {
setT("page2");
navigate('Page2'); }}>
// createStackNavigator
import {getT } from "globalScreen"
export default createStackNavigator(
{
Main: {
screen: Page1,
},
Page2: {
screen: Page2,
navigationOptions: ({ navigation }) => ({
headerTitle: getT(),
}),
},
},
{
initialRouteName: 'Main',
}
);

headerLeft issue of TabNavigator

Why my navigation does not display the icon that I've put on the header left parameter? It seems to me every thing is correct, the Icon is imported. Can you help to find the issue, here is my code:
const TabBarNavig = TabNavigator({
Places : {
screen :AddPlaces,
navigationOptions: ({ navigation }) => ({
title: 'Placements'
})
},
GetPlaces : GetPlaces,
New : New
});
TabBarNavig.navigationOptions = ({ navigation }) => {
const { state: { routes, index } } = navigation;
const navigationOptions = {};
navigationOptions.headerLeft = () => {
return (
<Icon
name = 'menu'
size = { 20}
color = 'white'
style={{paddingTop:20}}
/>
);
}
};
If you're trying to put the icon on top of the tab itself, you will have to use StackNavigator
const TabBarNavig = TabNavigator({
//your component here
})
const YNavigator = StackNavigator ({
Home:{screen: TabBarNavig,
navigationOptions: ({navigation}) => ({
headerLeft: <Icon name="menu" size={20} color="white" />,
})
},
})
if use react navigation version 3 you must use defaultNavigationOptions instead of navigationOptions
const TabBarNavig = TabNavigator({
Places : {
screen :AddPlaces,
navigationOptions: ({ navigation }) => ({
title: 'Placements'
})
},
GetPlaces : GetPlaces,
New : New
});
replace the TabNavigator with createBottomTabNavigator
const TabBarNavig = createBottomTabNavigator({
Places : {
screen :AddPlaces,
navigationOptions: ({ navigation }) => ({
title: 'Placements'
})
},
GetPlaces : GetPlaces,
New : New
});
I think this will solve your issue

React native navigation class function in header

I've got a problem with react native navigation and nested navigators.
Basically, the nested navigators (tab in a page) work pretty well. But when i add a button in the header with the _saveDetails function, it throw me an undefined function if i'm in the Players tab, and it works well when i'm on the Teams tab
Does anyone have an idea of what am i doing wrong? Thanks.
class HomeScreen extends React.Component {
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
headerRight: <Button title="Save" onPress={() =>
params.handleSave()} />
};
};
_saveDetails() {
console.log('clicked save');
}
componentDidMount() {
this.props.navigation.setParams({ handleSave: this._saveDetails });
}
render() {
return (
<View />
);
}
}
const MainScreenNavigator = TabNavigator({
Players: { screen: HomeScreen},
Teams: { screen: HomeScreen},
});
const SimpleApp = StackNavigator({
Home: { screen: MainScreenNavigator },
Player: { screen: PlayerPage },
});
Please try this one and let me know if you fetch any other problem. Just convert your code according to below code.
static navigationOptions = {
header: (navigation, header) => ({
...header,
right: (
navigation.navigate('Settings')}>
Settings
)
})
}
Thanks
Just change your code become like this
class HomeScreen extends React.Component {
static navigationOptions = ({ navigation }) => {
// const { params = {} } = navigation.state; //delete this
return {
headerRight: <Button title="Save" onPress={() =>
navigation.state.params.handleSave()} /> // add navigation.state
};
};
.....