Skip to content

Commit a28e59b

Browse files
Hedger WangFacebook Github Bot 2
Hedger Wang
authored and
Facebook Github Bot 2
committed
move propTypes, defaultProps, and childContextTypes into class.
Summary: This will make the code more readable. Reviewed By: ericvicenti Differential Revision: D3096663 fb-gh-sync-id: 540d2107ea3cd4c60aabdf7a6503c8c22bc4a985 fbshipit-source-id: 540d2107ea3cd4c60aabdf7a6503c8c22bc4a985
1 parent a7e5312 commit a28e59b

File tree

4 files changed

+81
-76
lines changed

4 files changed

+81
-76
lines changed

Libraries/CustomComponents/NavigationExperimental/NavigationCardStack.js

+12-17
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
*/
3333
'use strict';
3434

35-
const Animated = require('Animated');
3635
const NavigationAnimatedView = require('NavigationAnimatedView');
3736
const NavigationCard = require('NavigationCard');
3837
const NavigationCardStackStyleInterpolator = require('NavigationCardStackStyleInterpolator');
@@ -49,7 +48,6 @@ const {PropTypes} = React;
4948
const {Directions} = NavigationCardStackPanResponder;
5049

5150
import type {
52-
NavigationAnimatedValue,
5351
NavigationParentState,
5452
NavigationSceneRenderer,
5553
NavigationSceneRendererProps,
@@ -66,18 +64,6 @@ type Props = {
6664
renderScene: NavigationSceneRenderer,
6765
};
6866

69-
const propTypes = {
70-
direction: PropTypes.oneOf([Directions.HORIZONTAL, Directions.VERTICAL]),
71-
navigationState: NavigationPropTypes.navigationParentState.isRequired,
72-
renderOverlay: PropTypes.func,
73-
renderScene: PropTypes.func.isRequired,
74-
};
75-
76-
const defaultProps = {
77-
direction: Directions.HORIZONTAL,
78-
renderOverlay: emptyFunction.thatReturnsNull,
79-
};
80-
8167
/**
8268
* A controlled navigation view that renders a stack of cards.
8369
*
@@ -95,6 +81,18 @@ const defaultProps = {
9581
class NavigationCardStack extends React.Component {
9682
_renderScene : NavigationSceneRenderer;
9783

84+
static propTypes = {
85+
direction: PropTypes.oneOf([Directions.HORIZONTAL, Directions.VERTICAL]),
86+
navigationState: NavigationPropTypes.navigationParentState.isRequired,
87+
renderOverlay: PropTypes.func,
88+
renderScene: PropTypes.func.isRequired,
89+
};
90+
91+
static defaultProps = {
92+
direction: Directions.HORIZONTAL,
93+
renderOverlay: emptyFunction.thatReturnsNull,
94+
};
95+
9896
constructor(props: Props, context: any) {
9997
super(props, context);
10098
}
@@ -145,9 +143,6 @@ class NavigationCardStack extends React.Component {
145143
}
146144
}
147145

148-
NavigationCardStack.propTypes = propTypes;
149-
NavigationCardStack.defaultProps = defaultProps;
150-
151146
const styles = StyleSheet.create({
152147
animatedView: {
153148
flex: 1,

Libraries/NavigationExperimental/NavigationAnimatedView.js

+24-25
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,18 @@ type State = {
4444

4545
const {PropTypes} = React;
4646

47-
const propTypes = {
48-
applyAnimation: PropTypes.func,
49-
navigationState: NavigationPropTypes.navigationState.isRequired,
50-
onNavigate: PropTypes.func.isRequired,
51-
renderOverlay: PropTypes.func,
52-
renderScene: PropTypes.func.isRequired,
53-
};
54-
55-
const defaultProps = {
56-
applyAnimation: (
57-
position: NavigationAnimatedValue,
58-
navigationState: NavigationParentState,
59-
) => {
60-
Animated.spring(
61-
position,
62-
{
63-
bounciness: 0,
64-
toValue: navigationState.index,
65-
}
66-
).start();
67-
},
68-
};
47+
function applyDefaultAnimation(
48+
position: NavigationAnimatedValue,
49+
navigationState: NavigationParentState,
50+
): void {
51+
Animated.spring(
52+
position,
53+
{
54+
bounciness: 0,
55+
toValue: navigationState.index,
56+
}
57+
).start();
58+
}
6959

7060
class NavigationAnimatedView
7161
extends React.Component<any, Props, State> {
@@ -78,6 +68,18 @@ class NavigationAnimatedView
7868
props: Props;
7969
state: State;
8070

71+
static propTypes = {
72+
applyAnimation: PropTypes.func,
73+
navigationState: NavigationPropTypes.navigationState.isRequired,
74+
onNavigate: PropTypes.func.isRequired,
75+
renderOverlay: PropTypes.func,
76+
renderScene: PropTypes.func.isRequired,
77+
};
78+
79+
static defaultProps = {
80+
applyAnimation: applyDefaultAnimation,
81+
};
82+
8183
constructor(props: Props, context: any) {
8284
super(props, context);
8385

@@ -233,9 +235,6 @@ const styles = StyleSheet.create({
233235
},
234236
});
235237

236-
NavigationAnimatedView.propTypes = propTypes;
237-
NavigationAnimatedView.defaultProps = defaultProps;
238-
239238
NavigationAnimatedView = NavigationContainer.create(NavigationAnimatedView);
240239

241240
module.exports = NavigationAnimatedView;

Libraries/NavigationExperimental/NavigationRootContainer.js

+44-33
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313

1414
const AsyncStorage = require('AsyncStorage');
1515
const Linking = require('Linking');
16+
const NavigationPropTypes = require('NavigationPropTypes');
17+
const NavigationStateUtils = require('NavigationStateUtils');
1618
const Platform = require('Platform');
1719
const React = require('React');
18-
const NavigationPropTypes = require('NavigationPropTypes');
1920

2021
import type {
2122
NavigationAction,
23+
NavigationParentState,
2224
NavigationReducer,
2325
NavigationRenderer,
2426
} from 'NavigationTypeDefinition';
@@ -27,10 +29,6 @@ export type BackAction = {
2729
type: 'BackAction',
2830
};
2931

30-
function getBackAction(): BackAction {
31-
return { type: 'BackAction' };
32-
}
33-
3432
type Props = {
3533
/*
3634
* The default action to be passed into the reducer when getting the first
@@ -65,39 +63,56 @@ type Props = {
6563
renderNavigation: NavigationRenderer,
6664
};
6765

68-
const {PropTypes} = React;
69-
70-
const propTypes = {
71-
initialAction: NavigationPropTypes.action.isRequired,
72-
linkingActionMap: PropTypes.func,
73-
persistenceKey: PropTypes.string,
74-
reducer: PropTypes.func.isRequired,
75-
renderNavigation: PropTypes.func.isRequired,
66+
type State = {
67+
navState: ?NavigationParentState,
7668
};
7769

78-
const defaultProps = {
79-
initialAction: {
80-
type: 'RootContainerInitialAction',
81-
},
82-
};
70+
function getBackAction(): BackAction {
71+
return { type: 'BackAction' };
72+
}
73+
74+
const {PropTypes} = React;
8375

84-
class NavigationRootContainer extends React.Component {
76+
class NavigationRootContainer extends React.Component<any, Props, State> {
8577
_handleOpenURLEvent: Function;
8678

8779
props: Props;
80+
state: State;
81+
82+
static propTypes = {
83+
initialAction: NavigationPropTypes.action.isRequired,
84+
linkingActionMap: PropTypes.func,
85+
persistenceKey: PropTypes.string,
86+
reducer: PropTypes.func.isRequired,
87+
renderNavigation: PropTypes.func.isRequired,
88+
};
89+
90+
static defaultProps = {
91+
initialAction: { type: 'RootContainerInitialAction' },
92+
};
93+
94+
static childContextTypes = {
95+
onNavigate: PropTypes.func,
96+
};
8897

8998
constructor(props: Props) {
9099
super(props);
91-
this.handleNavigation = this.handleNavigation.bind(this);
92-
this._handleOpenURLEvent = this._handleOpenURLEvent.bind(this);
100+
93101
let navState = null;
94102
if (!this.props.persistenceKey) {
95-
navState = this.props.reducer(null, props.initialAction);
103+
navState = NavigationStateUtils.getParent(
104+
this.props.reducer(null, props.initialAction)
105+
);
96106
}
97107
this.state = { navState };
98108
}
99109

100-
componentDidMount() {
110+
componentWillMount(): void {
111+
this.handleNavigation = this.handleNavigation.bind(this);
112+
this._handleOpenURLEvent = this._handleOpenURLEvent.bind(this);
113+
}
114+
115+
componentDidMount(): void {
101116
if (this.props.LinkingActionMap) {
102117
Linking.getInitialURL().then(this._handleOpenURL.bind(this));
103118
Platform.OS === 'ios' && Linking.addEventListener('url', this._handleOpenURLEvent);
@@ -117,15 +132,17 @@ class NavigationRootContainer extends React.Component {
117132
}
118133
}
119134

120-
componentWillUnmount() {
121-
Platform.OS === 'ios' && Linking.removeEventListener('url', this._handleOpenURLEvent);
135+
componentWillUnmount(): void {
136+
if (Platform.OS === 'ios') {
137+
Linking.removeEventListener('url', this._handleOpenURLEvent);
138+
}
122139
}
123140

124-
_handleOpenURLEvent(event: {url: string}) {
141+
_handleOpenURLEvent(event: {url: string}): void {
125142
this._handleOpenURL(event.url);
126143
}
127144

128-
_handleOpenURL(url: ?string) {
145+
_handleOpenURL(url: ?string): void {
129146
if (!this.props.LinkingActionMap) {
130147
return;
131148
}
@@ -166,12 +183,6 @@ class NavigationRootContainer extends React.Component {
166183
}
167184
}
168185

169-
NavigationRootContainer.childContextTypes = {
170-
onNavigate: PropTypes.func,
171-
};
172-
173-
NavigationRootContainer.propTypes = propTypes;
174-
NavigationRootContainer.defaultProps = defaultProps;
175186
NavigationRootContainer.getBackAction = getBackAction;
176187

177188
module.exports = NavigationRootContainer;

Libraries/NavigationExperimental/NavigationTypeDefinition.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export type NavigationAnimationSetter = (
9595
) => void;
9696

9797
export type NavigationRenderer = (
98-
navigationState: NavigationState,
98+
navigationState: ?NavigationState,
9999
onNavigate: NavigationActionCaller,
100100
) => ReactElement;
101101

0 commit comments

Comments
 (0)