-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Guide around doing some action after re-focusing a screen #191
Comments
@brentvatne Thanks for answering.. but as i am new to react native not getting where to use this. Please help me with some example. |
@khushboogupta1 you can add your listener like below: componentDidMount() {
this._subscribe = this.props.navigation.addListener('didFocus', () => {
# do you update if need
});
} |
@MrPluto i am using stack navigator. According to your answer what i did is i called my API in 'didFocus', () { "HERE API CALL " }. But still no luck.. |
show some code ? |
@MrPluto Mailed you |
Hey Girls and Guys, i have also an problem with redux and react-navigation: here the structure: <App>
<Main>
<Header/> //this header has an number that displays the items inside the shopping cart value={this.props.cart.elements.length}
<MainStack>
<Home>
<ArtList>
<ShoppingCart>
</MainStack>
</Main>
<AddCart/> //Modal when saving the Article to the cart it goes Back to the MainStack (navigation.goBack())
</App> So if i add an item to the cart, the counter inside the header didnt changed. After switching between the stack-screens the number get updated. @MrPluto i tried the componentDidMount() but the problem is that inside my i have the latest version of redux, react-navigation and react-native. Any ideas? |
@khushboogupta1 i solved it, maybe you also can use it: i have a NavigationService where i handle all my NavigationActions (e.g. goBack(), navigate()...) when set the _navigator i also pass a function (in this case _goBackEvent). function setTopLevelNavigator(navigatorRef, goBackEvent) {
_navigator = navigatorRef;
_goBackEvent = goBackEvent;
} and this function i called in the goback function: function goBack(){
_navigator.dispatch(
NavigationActions.back()
)
if (_goBackEvent)
_goBackEvent();
} in my class where i added the Stack: <StackNav
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef, () => {
//do anything here when goBack is triggered
});
}}
/> |
In my case i use componentDidMount() {
this.subs = [
this.props.navigation.addListener('willFocus', () => {
}),
];
}
componentWillUnmount () {
this.subs.forEach(sub => sub.remove());
} It works very well to |
This is how I solved the problem of a page not refreshing after using goBack():
|
@alishaevn thank you it worked for me |
I'm struggling with hooks...the easiest I got is to do useEffect like
This works for history.goBack(), however, it also doubles server call when browser refreshes (one from useQuery and one from useEffect). To work around this problem, I added a global property "global.refreshOnGoBack" which is set to true before I call history.goBack(), and then
It seems to work fine, but I don't like it. Any better idea? |
This works for me, Using import { useIsFocused } from "@react-navigation/native";
const isFocused = useIsFocused();
useEffect(() => {
// do something
}, [isFocused]); |
Is there an equivalent of useIsFocused for non-native? @usama-asfar do you get useEffect called on initial load as well? So, is "do something" called twice on initial load? Another situation is that you may not always want to call "do something" when go back. For example, I have a popup dialog for editing. If I click Cancel button, it should go back without calling "do something" because nothing has changed (in my case "do something" is refetching data from server). |
@miliu99 that solution has some downsides like it invokes two times, for my case it should update its current state and re-render, it works fine. And fetching data to showing dialog won't work properly |
Thanks it worked for me also. |
This code is not working when use native back button. |
Cover both
withNavigationFocus
andaddListener
The text was updated successfully, but these errors were encountered: