Skip to content

Commit

Permalink
Add warning to setNativeProps and Animated for non-nested styles
Browse files Browse the repository at this point in the history
Summary: These were accidentally supported because they were merged at a lower
level. That's an implementation detail. setNativeProps should still
normalize the API.

I fixed some callers.

Setting props the normal way used to ignore these. Unfortunately we can't
warn for those cases because lots of extra props are spread. (The classic
transferPropsTo issue.)

@​public

Reviewed By: @vjeux

Differential Revision: D2514228

fb-gh-sync-id: 00ed6073827dc1924da20f3d80cbdf68d8a8a8fc
  • Loading branch information
sebmarkbage authored and facebook-github-bot-7 committed Oct 6, 2015
1 parent 82b3a6b commit 2ea3b93
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Libraries/Animated/src/AnimatedImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ var Interpolation = require('Interpolation');
var React = require('React');
var Set = require('Set');
var SpringConfig = require('SpringConfig');
var invariant = require('invariant');
var ViewStylePropTypes = require('ViewStylePropTypes');

var flattenStyle = require('flattenStyle');
var invariant = require('invariant');
var requestAnimationFrame = require('requestAnimationFrame');

import type InterpolationConfigType from 'Interpolation';
Expand Down Expand Up @@ -1078,6 +1079,19 @@ function createAnimatedComponent(Component: any): any {
);
}
}
AnimatedComponent.propTypes = {
style: function(props, propName, componentName) {
for (var key in ViewStylePropTypes) {
if (!Component.propTypes[key] && props[key] !== undefined) {
console.error(
'You are setting the style `{ ' + key + ': ... }` as a prop. You ' +
'should nest it in a style object. ' +
'E.g. `{ style: { ' + key + ': ... } }`'
);
}
}
}
};

return AnimatedComponent;
}
Expand Down
16 changes: 16 additions & 0 deletions Libraries/ReactIOS/NativeMethodsMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ type MeasureLayoutOnSuccessCallback = (
) => void


function warnForStyleProps(props, validAttributes) {
for (var key in validAttributes.style) {
if (!(validAttributes[key] || props[key] === undefined)) {
console.error(
'You are setting the style `{ ' + key + ': ... }` as a prop. You ' +
'should nest it in a style object. ' +
'E.g. `{ style: { ' + key + ': ... } }`'
);
}
}
}

var NativeMethodsMixin = {
measure: function(callback: MeasureOnSuccessCallback) {
RCTUIManager.measure(
Expand Down Expand Up @@ -66,6 +78,10 @@ var NativeMethodsMixin = {
* next render, they will remain active.
*/
setNativeProps: function(nativeProps: Object) {
if (__DEV__) {
warnForStyleProps(nativeProps, this.viewConfig.validAttributes);
}

var updatePayload = ReactNativeAttributePayload.create(
nativeProps,
this.viewConfig.validAttributes
Expand Down

0 comments on commit 2ea3b93

Please sign in to comment.