Skip to content

Commit

Permalink
Minimal update of Animated
Browse files Browse the repository at this point in the history
Ref #1576
  • Loading branch information
necolas committed Jun 26, 2020
1 parent 5dda71c commit cfe36d7
Showing 1 changed file with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { AnimatedEvent } from './AnimatedEvent';
import AnimatedProps from './nodes/AnimatedProps';
import React from 'react';
import invariant from 'fbjs/lib/invariant';
import setAndForwardRef from '../../../modules/setAndForwardRef';

function createAnimatedComponent(Component: any, defaultProps: any): any {
invariant(
Expand Down Expand Up @@ -40,10 +41,6 @@ function createAnimatedComponent(Component: any, defaultProps: any): any {
this._detachNativeEvents();
}

setNativeProps(props) {
this._component.setNativeProps(props);
}

UNSAFE_componentWillMount() {
this._attachProps(this.props);
}
Expand Down Expand Up @@ -143,6 +140,27 @@ function createAnimatedComponent(Component: any, defaultProps: any): any {
}
}

_setComponentRef = setAndForwardRef({
getForwardedRef: () => this.props.forwardedRef,
setLocalRef: ref => {
this._prevComponent = this._component;
this._component = ref;

// TODO: Delete this in a future release.
if (ref != null && ref.getNode == null) {
ref.getNode = () => {
console.warn(
'%s: Calling `getNode()` on the ref of an Animated component ' +
'is no longer necessary. You can now directly use the ref ' +
'instead. This method will be removed in a future release.',
ref.constructor.name ?? '<<anonymous>>',
);
return ref;
};
}
},
});

render() {
const props = this._propsAnimated.__getValue();
return (
Expand All @@ -158,22 +176,18 @@ function createAnimatedComponent(Component: any, defaultProps: any): any {
/>
);
}

_setComponentRef = c => {
this._prevComponent = this._component;
this._component = c;
};

// A third party library can use getNode()
// to get the node reference of the decorated component
getNode() {
return this._component;
}
}

const propTypes = Component.propTypes;

return AnimatedComponent;
return React.forwardRef(function AnimatedComponentWrapper(props, ref) {
return (
<AnimatedComponent
{...props}
{...(ref == null ? null : {forwardedRef: ref})}
/>
);
});
}

export default createAnimatedComponent;

0 comments on commit cfe36d7

Please sign in to comment.