Skip to content
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

Closed
brentvatne opened this issue Jun 12, 2018 · 17 comments
Closed

Guide around doing some action after re-focusing a screen #191

brentvatne opened this issue Jun 12, 2018 · 17 comments

Comments

@brentvatne
Copy link
Member

Cover both withNavigationFocus and addListener

@ghost
Copy link

ghost commented Jun 13, 2018

@brentvatne Thanks for answering.. but as i am new to react native not getting where to use this. Please help me with some example.

@pppluto
Copy link

pppluto commented Jun 13, 2018

@khushboogupta1 you can add your listener like below:

componentDidMount() {
    this._subscribe = this.props.navigation.addListener('didFocus', () => {
     # do you update if need
    });
}

@ghost
Copy link

ghost commented Jun 13, 2018

@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..

@pppluto
Copy link

pppluto commented Jun 13, 2018

show some code ?

@ghost
Copy link

ghost commented Jun 13, 2018

@MrPluto Mailed you

@MarcelZOI
Copy link

MarcelZOI commented Jul 4, 2018

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 <Main> this.props.navigation is undefined...

i have the latest version of redux, react-navigation and react-native.

Any ideas?

@MarcelZOI
Copy link

@khushboogupta1 i solved it, maybe you also can use it:

i have a NavigationService where i handle all my NavigationActions (e.g. goBack(), navigate()...)
(https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html)

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
      });
    }}
  />

@RodolfoSilva
Copy link

In my case i use willFocus:

componentDidMount() {
  this.subs = [
    this.props.navigation.addListener('willFocus', () => {
    }),
  ];
}

componentWillUnmount () {
  this.subs.forEach(sub => sub.remove());
}

It works very well to goBack action.

@alishaevn
Copy link

This is how I solved the problem of a page not refreshing after using goBack():

componentDidUpdate(prevProps) {
      if(prevProps.isFocused !== this.props.isFocused) {
        (insert whatever actions here that you need for the page to refresh. 
         for me it was dispatching the action that would "reload" my props)
      }
}

@amirhosseinkh2515
Copy link

@alishaevn thank you it worked for me

@miliu99
Copy link

miliu99 commented Jun 8, 2020

I'm struggling with hooks...the easiest I got is to do useEffect like

const { loading, data, refetch } = useQuery(Query_Data)

  useEffect(()=> {
      refetch()
  }, [refetch])

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

  useEffect(()=> {
    if (global.refreshOnGoBack) {
      refetch()
      global.refreshOnGoBack = false
    }
  }, [refetch])

It seems to work fine, but I don't like it. Any better idea?

@seyedasfar
Copy link

seyedasfar commented Jun 9, 2020

This works for me, Using useEffect with isFocused updates the current component state after goBack() invokes

import { useIsFocused } from "@react-navigation/native";

const isFocused = useIsFocused();
useEffect(() => {
 // do something
}, [isFocused]);

@miliu99
Copy link

miliu99 commented Jun 9, 2020

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).

@seyedasfar
Copy link

@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

@satya164
Copy link
Member

satya164 commented Jun 9, 2020

@satya164 satya164 closed this as completed Jun 9, 2020
@samimhakimi
Copy link

This works for me, Using useEffect with isFocused updates the current component state after goBack() invokes

import { useIsFocused } from "@react-navigation/native";

const isFocused = useIsFocused();
useEffect(() => {
 // do something
}, [isFocused]);

Thanks it worked for me also.

@WinnieWangs
Copy link

This works for me, Using useEffect with isFocused updates the current component state after goBack() invokes

import { useIsFocused } from "@react-navigation/native";

const isFocused = useIsFocused();
useEffect(() => {
 // do something
}, [isFocused]);

This code is not working when use native back button.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests