Skip to content

Commit dcd6113

Browse files
authored
Add adaptor to createAnimatedComponent() (#1988)
Here we can use an adaptor via setNativeProps to animate component outside the React lifecycle. The goal is to still be able to use the benefit of the Animated API even thought there are no performance benefits. A common example is to animate OpenGL node (bare webgl, gl-react, or react-three-fiber) on the JS thread: ```tsx import { Node } from "gl-react"; // In gl-react, updating the gl API outside React is done via setDrawProps() // In threejs, we would have a call on a prop by prop basis. // In webgl, we would use the gl object directly. const AnimatedNode = Animated.createAnimatedComponent(Node, { setNativeProps: (ref, props) => { ref.setDrawProps(props); }, }); ``` ## Description <!-- Description and motivation for this PR. Inlude Fixes #<number> if this is fixing some issue. Fixes # . --> ## Changes <!-- Please describe things you've changed here, make a **high level** overview, if change is simple you can omit this section. For example: - Added `foo` method which add bouncing animation - Updated `about.md` docs - Added caching in CI builds --> <!-- ## Screenshots / GIFs Here you can add screenshots / GIFs documenting your change. You can add before / after section if you're changing some behavior. ### Before ### After --> ## Test code and steps to reproduce <!-- Please include code that can be used to test this change and short description how this example should work. This snippet should be as minimal as possible and ready to be pasted into editor (don't exclude exports or remove "not important" parts of reproduction example) --> ## Checklist - [x] Included code example that can be used to test this change - [x] Updated TS types - [x] Added TS types tests - [ ] Added unit / integration tests - [ ] Updated documentation - [x] Ensured that CI passes
1 parent 2d068da commit dcd6113

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

react-native-reanimated.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,16 @@ declare module 'react-native-reanimated' {
247247
}
248248
export class Code extends Component<CodeProps> {}
249249

250+
type Options<P> = {
251+
setNativeProps: (ref: any, props: P) => void;
252+
}
250253
export function createAnimatedComponent<P extends object>(
251-
component: ComponentClass<P>
254+
component: ComponentClass<P>,
255+
options?: Options<P>
252256
): ComponentClass<AnimateProps<P>>;
253257
export function createAnimatedComponent<P extends object>(
254-
component: FunctionComponent<P>
258+
component: FunctionComponent<P>,
259+
options?: Options<P>
255260
): FunctionComponent<AnimateProps<P>>;
256261

257262
// classes

src/createAnimatedComponent.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function flattenArray(array) {
5757
return resultArr;
5858
}
5959

60-
export default function createAnimatedComponent(Component) {
60+
export default function createAnimatedComponent(Component, options = {}) {
6161
invariant(
6262
typeof Component !== 'function' ||
6363
(Component.prototype && Component.prototype.isReactComponent),
@@ -104,7 +104,7 @@ export default function createAnimatedComponent(Component) {
104104

105105
_attachNativeEvents() {
106106
const node = this._getEventViewRef();
107-
const viewTag = findNodeHandle(node);
107+
const viewTag = findNodeHandle(options.setNativeProps ? this : node);
108108

109109
for (const key in this.props) {
110110
const prop = this.props[key];
@@ -232,8 +232,12 @@ export default function createAnimatedComponent(Component) {
232232
}
233233

234234
_updateFromNative(props) {
235-
// eslint-disable-next-line no-unused-expressions
236-
this._component.setNativeProps?.(props);
235+
if (options.setNativeProps) {
236+
options.setNativeProps(this._component, props);
237+
} else {
238+
// eslint-disable-next-line no-unused-expressions
239+
this._component.setNativeProps?.(props);
240+
}
237241
}
238242

239243
_attachPropUpdater() {

0 commit comments

Comments
 (0)