Skip to content
This repository was archived by the owner on Apr 9, 2019. It is now read-only.

Commit 91a93bc

Browse files
committed
Add lifecycle events that hook into hiding and showing views during navigation transitions
1 parent 75a0a40 commit 91a93bc

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

examples/src/view.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ class View extends React.Component {
3131
counter: this.state.counter + 1
3232
});
3333
}
34+
componentDidMount() {
35+
console.log('component did mount', this.props.index);
36+
}
37+
navigationControllerWillShowView() {
38+
console.log('will show view', this.props.index);
39+
}
40+
navigationControllerDidShowView() {
41+
console.log('did show view', this.props.index);
42+
}
43+
navigationControllerWillHideView() {
44+
console.log('will hide view', this.props.index);
45+
}
46+
navigationControllerDidHideView() {
47+
console.log('did hide view', this.props.index);
48+
}
49+
componentWillUnmount() {
50+
console.log('component will unmount', this.props.index);
51+
}
3452
onNext() {
3553
const view = <View index={this.props.index+1} />;
3654
this.props.navigationController.pushView(view, {

src/navigation-controller.jsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,21 @@ class NavigationController extends React.Component {
206206
// Hide the previous view wrapper
207207
const prevViewWrapper = this[`__view-wrapper-${prev}`];
208208
prevViewWrapper.style.display = 'none';
209+
// Did hide view lifecycle event
210+
const prevView = this.refs['view-0'];
211+
if (prevView && typeof prevView.navigationControllerDidHideView === 'function') {
212+
prevView.navigationControllerDidHideView(this);
213+
}
214+
// Did show view lifecycle event
215+
const nextView = this.refs['view-1'];
216+
if (nextView && typeof nextView.navigationControllerDidShowView === 'function') {
217+
nextView.navigationControllerDidShowView(this);
218+
}
209219
// Unmount the previous view
210220
const mountedViews = [];
211221
mountedViews[prev] = null;
212222
mountedViews[next] = last(this.state.views);
223+
213224
this.setState({
214225
transition: null,
215226
mountedViews: mountedViews
@@ -254,6 +265,16 @@ class NavigationController extends React.Component {
254265
onComplete();
255266
}
256267
};
268+
// Will hide view lifecycle event
269+
const prevView = this.refs['view-0'];
270+
if (prevView && typeof prevView.navigationControllerWillHideView === 'function') {
271+
prevView.navigationControllerWillHideView(this);
272+
}
273+
// Will show view lifecycle event
274+
const nextView = this.refs['view-1'];
275+
if (nextView && typeof nextView.navigationControllerWillShowView === 'function') {
276+
nextView.navigationControllerWillShowView(this);
277+
}
257278
// Built-in transition
258279
if (typeof transition === 'number') {
259280
// Manually transition the views
@@ -281,7 +302,7 @@ class NavigationController extends React.Component {
281302
if (typeof transition === 'function') {
282303
const [prev,next] = this.__viewIndexes;
283304
const prevView = this[`__view-wrapper-${prev}`];
284-
const nextView = this[`__view-wrapper-${next}`];
305+
const nextView = this[`__view-wrapper-${next}`];
285306
transition(prevView, nextView, () => {
286307
this.__animateViewsComplete();
287308
this.__transitionViewsComplete();

0 commit comments

Comments
 (0)