react-native-router-flux: onEnter/onExit hooks inside scenes components
Is there any simple way to have onEnter
/ onExit
hooks inside navigation scenes components? It’s very useful to have it because it gives the ability to create fancy animation effects.
At the moment I can see the only solution is passing props from root component and then process them inside componentWillReceiveProps
of scenes component but it’s super-ugly. Maybe there is some easy way to handle it with callbacks, something like <Scene onEnter={() => this.sceneComponentRef.onEnter()} />
?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 33 (16 by maintainers)
@aksonov Can you provide a sample code using
onEnter
inside a component? Thanksi’ve tried all suggestion here but failed, because my case is inside a tabs. recently i found solution from https://stackoverflow.com/questions/49005289/reload-screen-when-the-tab-is-changed to use didFocus listener. here is the code:
@luco it is not implemented yet, but it is not difficult to add, check sources (within
createWrapper
function), feel free to submit PR.This can be achieved refresh function,
Actions.refresh ({action: new Date (). GetTime ()});
The value passed must be changed, otherwise it will not trigger thecomponentWillReceiveProps
event Now I found that this method works only on android, ios does not workThis is starting to work for me after adding a static onExit method to my component.
I see ‘onExit’ in logs for both Android and iOS.
I am still trying to figure out how I can get to the component instance and component state from the component’s static onExit method?
Using:
@aksonov - I know you said that using componentDidMount is an anti-pattern, but it’s the only easy way right now to actually modify the navbar. I’m pretty sure other users are doing the following:
I’m actually thinking of completely giving up on the build-in navBar as it is too complicated to change the title and what the buttons do. Rendering a custom Navbar in each component looks like less work right now.
Not sure if this will help but I solved this in my case where i wanted an api to be called every time i enter a screen that has already been mounted:
Where searchItems is a redux action mapped using
connect(mapStateToProps, {searchItems})
in the same componentYoohooo! We have latest React Navigation release with exciting blur/focus events system. No more hacks and inconsistent onEnter/onExit behavior, we just need to adopt the new system to RNRF.
@aksonov Thanks a lot for recent onEnter / onExit as scene’s instances methods implementation. Unfortunately,
onEnter
behavior is little bit of quirky - it works only when component is mounted(created first time), but when, for-example, a user navigates to already created scene, the scene is entered butonEnter
is not invoked so it may be misleading… Hope what I’ve described makes sense… 😺Same thing would be very useful for
onLeft
&onRight
(onBack
?) callbacks@aksonov Thanks a lot for hint. Works great for
onExit
hook, but unfortunately, there is no component instance inActions.refs
yet ononEnter
hook. Is there any workaround?Yes, use
Actions.refs.SCENE_NAME
to refer to your component instanceYou can try to use
Actions.refs.SCENE_NAME
to access component instance@aksonov ( Yes, use Actions.refs.SCENE_NAME to refer to your component instance)
can you show me the code please? I still have no idea
You are right… Need to think how to implement it in better way…
if i have two same screen in my routestack,for example A B A C,when i back A from C, how can i discriminate which A i focus?
can i use define non-static onEnter/onExit methods now?