Skip to content

Commit 5876052

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Make setting useNativeDriver required. Add runtime warning if not specified
Summary: We found that many callsites existed that could be using the native driver, but weren't. In order to help people use it when appropriate and eventually switch the default, we are requiring that useNativeDriver is explicit, even when set to false. This change adds a runtime warning if useNativeDriver is not specified, hopefully giving some light feedback to remember to use the native driver when you can. Without it being explicit it is very easy to forget setting this. Reviewed By: JoshuaGross Differential Revision: D17575918 fbshipit-source-id: e54612d87177e1821692b7de20fe673df0e890d2
1 parent 62acf6e commit 5876052

File tree

10 files changed

+29
-9
lines changed

10 files changed

+29
-9
lines changed

Libraries/Animated/src/AnimatedEvent.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const {shouldUseNativeDriver} = require('./NativeAnimatedHelper');
2020
export type Mapping = {[key: string]: Mapping} | AnimatedValue;
2121
export type EventConfig = {
2222
listener?: ?Function,
23-
useNativeDriver?: boolean,
23+
useNativeDriver: boolean,
2424
};
2525

2626
function attachNativeEvent(
@@ -87,8 +87,14 @@ class AnimatedEvent {
8787
};
8888
__isNative: boolean;
8989

90-
constructor(argMapping: Array<?Mapping>, config?: EventConfig = {}) {
90+
constructor(argMapping: Array<?Mapping>, config: EventConfig) {
9191
this._argMapping = argMapping;
92+
93+
if (config == null) {
94+
console.warn('Animated.event now requires a second argument for options');
95+
config = {};
96+
}
97+
9298
if (config.listener) {
9399
this.__addListener(config.listener);
94100
}

Libraries/Animated/src/AnimatedImplementation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ function unforkEvent(
508508
}
509509
}
510510

511-
const event = function(argMapping: Array<?Mapping>, config?: EventConfig): any {
511+
const event = function(argMapping: Array<?Mapping>, config: EventConfig): any {
512512
const animatedEvent = new AnimatedEvent(argMapping, config);
513513
if (animatedEvent.__isNative) {
514514
return animatedEvent;

Libraries/Animated/src/AnimatedMock.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const loop = function(
119119
return emptyAnimation;
120120
};
121121

122-
const event = function(argMapping: Array<?Mapping>, config?: EventConfig): any {
122+
const event = function(argMapping: Array<?Mapping>, config: EventConfig): any {
123123
return null;
124124
};
125125

Libraries/Animated/src/NativeAnimatedHelper.js

+7
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ function assertNativeAnimatedModule(): void {
265265
let _warnedMissingNativeAnimated = false;
266266

267267
function shouldUseNativeDriver(config: AnimationConfig | EventConfig): boolean {
268+
if (config.useNativeDriver == null) {
269+
console.warn(
270+
'Animated: `useNativeDriver` was not specified. This is a required ' +
271+
'option and must be explicitly set to `true` or `false`',
272+
);
273+
}
274+
268275
if (config.useNativeDriver === true && !NativeAnimatedModule) {
269276
if (!_warnedMissingNativeAnimated) {
270277
console.warn(

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExApp.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ class Circle extends React.Component<any, any> {
6565
this.setState(
6666
{
6767
panResponder: PanResponder.create({
68-
onPanResponderMove: Animated.event([
69-
null, // native event - ignore (step1: uncomment)
70-
{dx: this.state.pan.x, dy: this.state.pan.y}, // links pan to gestureState (step1: uncomment)
71-
]),
68+
onPanResponderMove: Animated.event(
69+
[
70+
null, // native event - ignore (step1: uncomment)
71+
{dx: this.state.pan.x, dy: this.state.pan.y}, // links pan to gestureState (step1: uncomment)
72+
],
73+
{useNativeDriver: false},
74+
),
7275
onPanResponderRelease: (e, gestureState) => {
7376
LayoutAnimation.easeInEaseOut(); // @flowfixme animates layout update as one batch (step3: uncomment)
7477
Animated.spring(this.state.pop, {

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExBobble.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class AnExBobble extends React.Component<Object, any> {
8686
},
8787
onPanResponderMove: Animated.event(
8888
[null, {dx: this.state.bobbles[0].x, dy: this.state.bobbles[0].y}],
89-
{listener: bobblePanListener}, // async state changes with arbitrary logic
89+
{listener: bobblePanListener, useNativeDriver: false}, // async state changes with arbitrary logic
9090
),
9191
onPanResponderRelease: releaseBobble,
9292
onPanResponderTerminate: releaseBobble,

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExChained.js

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class AnExChained extends React.Component<Object, any> {
6363
},
6464
onPanResponderMove: Animated.event(
6565
[null, {dx: this.state.stickers[0].x, dy: this.state.stickers[0].y}], // map gesture to leader
66+
{useNativeDriver: false},
6667
),
6768
onPanResponderRelease: releaseChain,
6869
onPanResponderTerminate: releaseChain,

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExScroll.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class AnExScroll extends React.Component<$FlowFixMeProps, any> {
3333
scrollEventThrottle={16 /* get all events */}
3434
onScroll={Animated.event(
3535
[{nativeEvent: {contentOffset: {x: this.state.scrollX}}}], // nested event mapping
36+
{useNativeDriver: false},
3637
)}
3738
contentContainerStyle={{flex: 1, padding: 10}}
3839
pagingEnabled={true}

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExSet.js

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class AnExSet extends React.Component<Object, any> {
8888
},
8989
onPanResponderMove: Animated.event(
9090
[null, {dy: this.state.dismissY}], // track pan gesture
91+
{useNativeDriver: false},
9192
),
9293
onPanResponderRelease: (e, gestureState) => {
9394
if (gestureState.dy > 100) {

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExTilt.js

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class AnExTilt extends React.Component<Object, any> {
3838
},
3939
onPanResponderMove: Animated.event(
4040
[null, {dx: this.state.panX}], // panX is linked to the gesture
41+
{useNativeDriver: false},
4142
),
4243
onPanResponderRelease: (e, gestureState) => {
4344
let toValue = 0;

0 commit comments

Comments
 (0)