Workiva / react-dart

Dart Bindings for React JS
BSD 2-Clause "Simplified" License
413 stars 67 forks source link

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

Closed aaronlademann-wf closed 4 years ago

aaronlademann-wf commented 4 years ago

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);
  });
}
greglittlefield-wf commented 4 years ago

QA +1