Skip to content

Commit 26734a8

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Migrating View to be a Flow Typed ES6 class
Summary: The flow type for View using createReactClass was essentially `any`, allowing any Prop to be passed in, only pseudo enforced at run time via propTypes. This diff converts View away from createReactClass and instead uses ReactNative.NativeComponent. This was previously typed as any as well which didn't buy us much. This change converts View to be an ES6 React class component to ensure proptypechecking, and exposes the methods copied from NativeMethodsMixin. Reviewed By: yungsters Differential Revision: D5933888 fbshipit-source-id: eae63b818203e0e86741f9f154ec9cf3498369e2
1 parent 1ff1e57 commit 26734a8

File tree

3 files changed

+29
-58
lines changed

3 files changed

+29
-58
lines changed

Libraries/Components/View/View.js

+13-29
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
*/
1111
'use strict';
1212

13-
const NativeMethodsMixin = require('NativeMethodsMixin');
1413
const Platform = require('Platform');
1514
const React = require('React');
15+
const ReactNative = require('ReactNative');
1616
const ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
1717
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
1818
const ViewPropTypes = require('ViewPropTypes');
1919
const {ViewContextTypes} = require('ViewContext');
20-
21-
const createReactClass = require('create-react-class');
2220
const invariant = require('fbjs/lib/invariant');
2321
const requireNativeComponent = require('requireNativeComponent');
2422

@@ -28,40 +26,26 @@ import type {ViewChildContext} from 'ViewContext';
2826
export type Props = ViewProps;
2927

3028
/**
31-
* The most fundamental component for building a UI.
29+
* The most fundamental component for building a UI, View is a container that
30+
* supports layout with flexbox, style, some touch handling, and accessibility
31+
* controls.
3232
*
33-
* See http://facebook.github.io/react-native/docs/view.html
33+
* @see http://facebook.github.io/react-native/docs/view.html
3434
*/
35-
const View = createReactClass({
36-
displayName: 'View',
37-
// TODO: We should probably expose the mixins, viewConfig, and statics publicly. For example,
38-
// one of the props is of type AccessibilityComponentType. That is defined as a const[] above,
39-
// but it is not rendered by the docs, since `statics` below is not rendered. So its Possible
40-
// values had to be hardcoded.
41-
mixins: [NativeMethodsMixin],
42-
43-
// `propTypes` should not be accessed directly on View since this wrapper only
44-
// exists for DEV mode. However it's important for them to be declared.
45-
// If the object passed to `createClass` specifies `propTypes`, Flow will
46-
// create a static type from it.
47-
propTypes: ViewPropTypes,
35+
class View extends ReactNative.NativeComponent<Props> {
36+
static propTypes = ViewPropTypes;
37+
static childContextTypes = ViewContextTypes;
4838

49-
/**
50-
* `NativeMethodsMixin` will look for this when invoking `setNativeProps`. We
51-
* make `this` look like an actual native component class.
52-
*/
53-
viewConfig: {
39+
viewConfig = {
5440
uiViewClassName: 'RCTView',
5541
validAttributes: ReactNativeViewAttributes.RCTView,
56-
},
57-
58-
childContextTypes: ViewContextTypes,
42+
};
5943

6044
getChildContext(): ViewChildContext {
6145
return {
6246
isInAParentText: false,
6347
};
64-
},
48+
}
6549

6650
render() {
6751
invariant(
@@ -74,8 +58,8 @@ const View = createReactClass({
7458
// adding functionality this component that you'd want to be available in both
7559
// dev and prod modes.
7660
return <RCTView {...this.props} />;
77-
},
78-
});
61+
}
62+
}
7963

8064
const RCTView = requireNativeComponent('RCTView', View, {
8165
nativeOnly: {

Libraries/Components/View/View.js.flow

-28
This file was deleted.

Libraries/Renderer/shims/ReactNativeTypes.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,27 @@ type SecretInternalsType = {
8686
// And how much information to fill in for the above types.
8787
};
8888

89+
declare class ReactNativeComponent<Props, State = void>
90+
extends React$Component<Props, State> {
91+
92+
blur(): void,
93+
focus(): void,
94+
measure(callback: MeasureOnSuccessCallback): void,
95+
measureInWindow(callback: MeasureInWindowOnSuccessCallback): void,
96+
measureLayout(
97+
relativeToNativeNode: number,
98+
onSuccess: MeasureLayoutOnSuccessCallback,
99+
onFail: () => void,
100+
): void,
101+
setNativeProps(nativeProps: Object): void,
102+
}
103+
89104
/**
90105
* Flat ReactNative renderer bundles are too big for Flow to parse efficiently.
91106
* Provide minimal Flow typing for the high-level RN API and call it a day.
92107
*/
93108
export type ReactNativeType = {
94-
NativeComponent: any,
109+
NativeComponent: typeof ReactNativeComponent,
95110
findNodeHandle(componentOrHandle: any): ?number,
96111
render(
97112
element: React$Element<any>,

0 commit comments

Comments
 (0)