React-native-router-flux: Some helpful tips on using the on Methods [Router flux v4.0.6]

Created on 18 Nov 2018  路  6Comments  路  Source: aksonov/react-native-router-flux

Using Static on Methods with HOCs
This is just a helpful tip for anyone who use the onExit/onEnter methods as a static method in their Component Class. Please refer to this link https://reactjs.org/docs/higher-order-components.html.

  • If your Scene Components are Wrapped in Custom HOCs/ Decorators, then the static onExit/onEnter methods will not work as your Custom HOCs will not copy the static methods over to your Enhanced Component.

  • Use this npm package called hoist-non-react-statics to copy your Component level static methods over to your Enhanced Component.

Implement onBack from your Scene and not while declaring the Scene

  • If you have a Scene where in you want to make some changes to your Component State when Back button is pressed, then doing this <Scene key={...} component={...} onBack={()=>/*code*/}/> will not help.

  • <Scene key={...} component={...} onBack={()=>/*code*/} back={true}/> Make sure back = true is passed to your scene, now in your Component's lifecycle add this

componentDidMount(){
    setTimeout(()=> {
        Actions.refresh({onBack:()=>this.changeSomethingInYourComponent()})
    })
}
  • setTimeout is added because without setTimeout props of the prev screen get refreshed, may be because it waits for the animation to complete, so better to run it on a fresh queue.(Clarification required)
documentation

All 6 comments

I want to create the private scene which redirects User to the login page
Can you make an example, please?

In case of animations and refresh props, it's better to use
const params = { onBack: this.onBack, onRight: this.onRight } InteractionManager.runAfterInteractions(() => { Actions.refresh(params); });

because

InteractionManager allows long-running work to be scheduled after any interactions/animations have completed

Yes InteracionManager is a better way to do it than setTimeout

@shubhang93 Great tips, thanks! It would be great if you can submit documentation PR

@shubhang93 Great tips, thanks! It would be great if you can submit documentation PR

PR submitted, please go through it.

@mahyar33 did you ever figure out how to do that?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

llgoer picture llgoer  路  3Comments

jgibbons picture jgibbons  路  3Comments

GCour picture GCour  路  3Comments

willmcclellan picture willmcclellan  路  3Comments

xnog picture xnog  路  3Comments