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

Commit 7f90c5e

Browse files
committed
Merge branch 'lifecycle-events'
2 parents cd46048 + 91a93bc commit 7f90c5e

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
@@ -213,10 +213,21 @@ class NavigationController extends React.Component {
213213
// Hide the previous view wrapper
214214
const prevViewWrapper = this[`__view-wrapper-${prev}`];
215215
prevViewWrapper.style.display = 'none';
216+
// Did hide view lifecycle event
217+
const prevView = this.refs['view-0'];
218+
if (prevView && typeof prevView.navigationControllerDidHideView === 'function') {
219+
prevView.navigationControllerDidHideView(this);
220+
}
221+
// Did show view lifecycle event
222+
const nextView = this.refs['view-1'];
223+
if (nextView && typeof nextView.navigationControllerDidShowView === 'function') {
224+
nextView.navigationControllerDidShowView(this);
225+
}
216226
// Unmount the previous view
217227
const mountedViews = [];
218228
mountedViews[prev] = null;
219229
mountedViews[next] = last(this.state.views);
230+
220231
this.setState({
221232
transition: null,
222233
mountedViews: mountedViews
@@ -261,6 +272,16 @@ class NavigationController extends React.Component {
261272
onComplete();
262273
}
263274
};
275+
// Will hide view lifecycle event
276+
const prevView = this.refs['view-0'];
277+
if (prevView && typeof prevView.navigationControllerWillHideView === 'function') {
278+
prevView.navigationControllerWillHideView(this);
279+
}
280+
// Will show view lifecycle event
281+
const nextView = this.refs['view-1'];
282+
if (nextView && typeof nextView.navigationControllerWillShowView === 'function') {
283+
nextView.navigationControllerWillShowView(this);
284+
}
264285
// Built-in transition
265286
if (typeof transition === 'number') {
266287
// Manually transition the views
@@ -288,7 +309,7 @@ class NavigationController extends React.Component {
288309
if (typeof transition === 'function') {
289310
const [prev,next] = this.__viewIndexes;
290311
const prevView = this[`__view-wrapper-${prev}`];
291-
const nextView = this[`__view-wrapper-${next}`];
312+
const nextView = this[`__view-wrapper-${next}`];
292313
transition(prevView, nextView, () => {
293314
this.__animateViewsComplete();
294315
this.__transitionViewsComplete();

0 commit comments

Comments
 (0)