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

How to refresh Tabs scene every time i switch tabs? #2937

Closed
AndrejZdravev opened this issue Mar 13, 2018 · 5 comments
Closed

How to refresh Tabs scene every time i switch tabs? #2937

AndrejZdravev opened this issue Mar 13, 2018 · 5 comments

Comments

@AndrejZdravev
Copy link

Version

Tell us which versions you are using:

  • "react-native-router-flux": "^4.0.0-beta.28"
  • "react-native": "0.54.0",
@asciifaceman
Copy link
Contributor

asciifaceman commented Mar 13, 2018

onEnter:

In stack definition:

                    <Scene key="SomeView"
                           component={SomeView}
                           onEnter={this.onEnterSomeView}
                    />

In SomeView:

export default class SomeView extends Component {

  static onEnterSomeView = () => {
    Actions.refresh({
      enterTime: new Date()
    });
  }

  componentWillReceiveProps (nextProps) {
    console.log(this.props.enterTime);
    console.log(nextProps.enterTime);   // for you to see what is happening
    if (this.props.enterTime !== nextProps.enterTime) {
      this.setState({something: nextProps.enterTime}); // a function that changes the state to re-render
    }
  }

  render() {
    return (
        <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
            <Text>{this.state.something}</Text>
        </View>
    )
}

What is going on here is that when you navigate to a View, onEnter is fired. This even works if you are going back and forth between tabs. The refresh line refreshes the view setting enterTime to that time. If there is a delta between enterTime and the and the next enterTime, it will fire whatever is in that function or whatever you put there.

This can be expensive if you are doing heavy lifting.

You also have to change the state (setState) of something that is rendered to trigger it. (someone could correct me here, but I'm 90% sure you have to change state of something rendered)

@AndrejZdravev
Copy link
Author

Thanks @asciifaceman for your help, but when i use this code i get warning "Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op."

@asciifaceman
Copy link
Contributor

What are you doing specifically?

@aaron25mt
Copy link

You might be clicking too fast, I get that sometimes but usually works fine using the Actions.refresh method.

@aksonov
Copy link
Owner

aksonov commented Aug 8, 2018

Please try to reproduce it with Example project and latest version 4.0.0-beta.40. Feel free to open if the issue still exists

@aksonov aksonov closed this as completed Aug 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants