-
-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to call component methods on press of tab using tabBarOnPress #2725
Comments
Depending on where your TabScreen lives within your full app, you may want to utilize screen tracking to check which tab is currently active. That way, instead of declaring
|
I've already tried that. I'll give you an example of what I'm trying to achieve. That's why I was trying to use tabBarOnPress instead to achieve the same functionality. Using your example: // gets the current screen from navigation state
function getCurrentRouteName(navigationState) {
if (!navigationState) {
return null;
}
const route = navigationState.routes[navigationState.index];
// dive into nested navigators
if (route.routes) {
return getCurrentRouteName(route);
}
return route.routeName;
}
function callServer() {
console.log("Call server");
//Code to make ajax call
this.setState({data: response.data}); //this causes a re render of the tabs
}
function generateTabs(){
//tabArrayList is a dynamically generated array of objects.
//Each object contains title of tab and other information
return tabArrayList.reduce((routes,index, tabArrayList) => {
routes[index] = {
screen: TabScreen,
navigationOptions: {
title: tabArrayList[index].title //sets the title for each tab
}
};
return routes;
},{});
}
const AppNavigator = TabNavigator(generateTabs());
export default () => (
<AppNavigator
onNavigationStateChange={(prevState, currentState) => {
const currentScreen = getCurrentRouteName(currentState);
const prevScreen = getCurrentRouteName(prevState);
if (prevScreen !== currentScreen) {
callServer(); //need to call server on press of every tab
}
}}
/>
); |
I was able to solve my problem but I'll keep this issue open as it's still not possible to call component methods inside tabBarOnPress. |
@sanjayholla94 how did you solve your problem? |
Looks like this is already solved but FWIW a slightly different syntax from @sanjayholla94 did work fine for me:
|
Here works for me with redux.
Hope this helps! |
@lucascarvalho Hey, sorry for the late response! I was really busy with project deadlines over the past few weeks. I'll post an expo snack over the weekend and also check if it's still an issue. |
thanks for helping each other out here! i've made an issue on website repo, we should document this react-navigation/react-navigation.github.io#34 |
Thanks to @jwrubel , I had changed few lines, then the below code is working for me.
|
@jwrubel how could I use your workaround when I integrating redux with navigation.? |
@jsina I'm not using the redux integration in this package but you should be able to invoke redux actions from your callback. Just a small-line change to my pseudocode above:
Is this what you're referring to? |
@jwrubel Not work for me! Some one has same issue? |
Yes, same issue. In @jwrubel example, i tried to replace onFocus() with Foo(), and i get: Cannot read property 'Foo' of undefined |
My code above was written against version 1.0.0 of this library. There have been many changes to this library since this past August. It might not work against the current version of react-navigation. I haven't updated my library recently - sorry I cannot be of more help. |
I am currently using version 1.1.147 (steering clear of the newer libraries). I'm trying to run my imagepicker function everytime the Tab is clicked. So everytime tabBarOnPress is in play, i want to launch my own function. Not sure how to do this |
Current Behavior
I'm trying to call component methods inside tabBarOnPress by setting it via navigation.setParams and the function call throws the following error the first time it is pressed: Cannot read property 'callServer' of undefined. This works from the second time the same tab is pressed.
This is because the navigation state params is set/updated after the first call to tabBarOnPress. How do I make the server request the first time a tab is pressed?
Expected Behavior
Should be able to call components the first time a tab is pressed
Your Environment
The text was updated successfully, but these errors were encountered: