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

Proxy event.persist() when wrapping synthetic JS events #263

Merged
merged 1 commit into from
Jul 21, 2020

Conversation

aaronlademann-wf
Copy link
Collaborator

I'm resurrecting #249, as I've run into a more concrete use-case for event.persist() than the one that prompted #249.

Since component updates are not guaranteed to be synchronous in React 16+, the optional callback for setState may be called asynchronously on the JS side of things. This means that doing something like passing a SyntheticEvent to a consumer callback within that callback will result in ReactJS spitting out a big ugly warning about the event having been recycled.

Without persist()

void handleClick(SyntheticMouseEvent event) {
  setState({}, () {
    // Ruh roh Scoobs... this could be recycled when they access it.
    props.someConsumerCallbackThatNeedsTheEvent(event);
  });
}

With persist()

void handleClick(SyntheticMouseEvent event) {
  event.persist();
  setState({}, () {
    // All good... event is persisted and accessible for the consumer.
    props.someConsumerCallbackThatNeedsTheEvent(event);
  });
}

Copy link
Collaborator

@greglittlefield-wf greglittlefield-wf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@greglittlefield-wf
Copy link
Collaborator

QA +1

@greglittlefield-wf greglittlefield-wf merged commit 784fa39 into master Jul 21, 2020
@aaronlademann-wf aaronlademann-wf deleted the forwardRef/fix-event-persist-interop branch July 22, 2020 14:28
aaronlademann-wf added a commit that referenced this pull request Jul 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants