Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make animated component's event tag properly update #6030

Merged
merged 10 commits into from
Jun 11, 2024
41 changes: 33 additions & 8 deletions src/createAnimatedComponent/createAnimatedComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ export function createAnimatedComponent(
}

componentDidMount() {
this._setComponentViewTag();
this._setEventViewTag();
this._componentViewTag = this._getComponentViewTag();
this._eventViewTag = this._getEventViewTag();
tjzel marked this conversation as resolved.
Show resolved Hide resolved
this._attachNativeEvents();
this._jsPropsUpdater.addOnJSPropsChangeListener(this);
this._attachAnimatedStyles();
Expand Down Expand Up @@ -226,22 +226,24 @@ export function createAnimatedComponent(
}
}

_setComponentViewTag() {
this._componentViewTag = this._getViewInfo().viewTag as number;
_getComponentViewTag() {
return this._getViewInfo().viewTag as number;
}

_setEventViewTag() {
// Setting the tag for registering events - since the event emitting view can be nested inside the main component
_getEventViewTag() {
// Get the tag for registering events - since the event emitting view can be nested inside the main component
const componentAnimatedRef = this._component as AnimatedComponentRef;
let newTag: number;
if (componentAnimatedRef.getScrollableNode) {
const scrollableNode = componentAnimatedRef.getScrollableNode();
this._eventViewTag = findNodeHandle(scrollableNode) ?? -1;
newTag = findNodeHandle(scrollableNode) ?? -1;
} else {
this._eventViewTag =
newTag =
findNodeHandle(
options?.setNativeProps ? this : componentAnimatedRef
) ?? -1;
}
return newTag;
}

_attachNativeEvents() {
Expand Down Expand Up @@ -291,6 +293,29 @@ export function createAnimatedComponent(
_updateNativeEvents(
prevProps: AnimatedComponentProps<InitialComponentProps>
) {
// If the event view tag changes, we need to completely re-mount all events
const computedEventTag = this._getEventViewTag();
szydlovsky marked this conversation as resolved.
Show resolved Hide resolved
if (this._eventViewTag !== computedEventTag) {
// Remove all bindings from previous props that ran on the old viewTag
for (const key in prevProps) {
const prevProp = prevProps[key];
if (
has('workletEventHandler', prevProp) &&
prevProp.workletEventHandler instanceof WorkletEventHandler
) {
prevProp.workletEventHandler.unregisterFromEvents(
this._eventViewTag
);
}
}
szydlovsky marked this conversation as resolved.
Show resolved Hide resolved
// We don't need to unregister from current (new) props, because their events weren't registered yet
// Replace the view tag
this._eventViewTag = computedEventTag;
// Attach the events with a new viewTag
this._attachNativeEvents();
return;
}

for (const key in prevProps) {
const prevProp = prevProps[key];
if (
Expand Down
Loading