官网参考1

暂时没看明白!
This API can be used to subscribe to a variety of “subscribable” sources, from event dispatchers to RxJS observables. Below are a few examples of how to subscribe to common types.

Subscribing to event dispatchers

路径:github\react\packages\create-subscription\README.md
Below is an example showing how create-subscription can be used to subscribe to event dispatchers such as DOM elements.

  1. import React from "react";
  2. import { createSubscription } from "create-subscription";
  3. // Start with a simple component.
  4. // In this case, it's a function component, but it could have been a class.
  5. function FollowerComponent({ followersCount }) {
  6. return <div>You have {followersCount} followers!</div>;
  7. }
  8. // Create a wrapper component to manage the subscription.
  9. const EventHandlerSubscription = createSubscription({
  10. getCurrentValue: eventDispatcher => eventDispatcher.value,
  11. subscribe: (eventDispatcher, callback) => {
  12. const onChange = event => callback(eventDispatcher.value);
  13. eventDispatcher.addEventListener("change", onChange);
  14. return () => eventDispatcher.removeEventListener("change", onChange);
  15. }
  16. });
  17. // Your component can now be used as shown below.
  18. // In this example, 'eventDispatcher' represents a generic event dispatcher.
  19. <EventHandlerSubscription source={eventDispatcher}>
  20. {value => <FollowerComponent followersCount={value} />}
  21. </EventHandlerSubscription>;

参考2

路径:reactjs.org\examples\update-on-async-rendering\adding-event-listeners-create-subscription.js

adding-event-listeners-before

  1. // Before
  2. class ExampleComponent extends React.Component {
  3. // highlight-range{1-10}
  4. componentWillMount() {
  5. this.setState({
  6. subscribedValue: this.props.dataSource.value,
  7. });
  8. // This is not safe; it can leak!
  9. this.props.dataSource.subscribe(
  10. this.handleSubscriptionChange
  11. );
  12. }
  13. componentWillUnmount() {
  14. this.props.dataSource.unsubscribe(
  15. this.handleSubscriptionChange
  16. );
  17. }
  18. handleSubscriptionChange = dataSource => {
  19. this.setState({
  20. subscribedValue: dataSource.value,
  21. });
  22. };
  23. }

adding-event-listeners-after

  1. // After
  2. class ExampleComponent extends React.Component {
  3. // highlight-range{1-3}
  4. state = {
  5. subscribedValue: this.props.dataSource.value,
  6. };
  7. // highlight-line
  8. // highlight-range{1-18}
  9. componentDidMount() {
  10. // Event listeners are only safe to add after mount,
  11. // So they won't leak if mount is interrupted or errors.
  12. this.props.dataSource.subscribe(
  13. this.handleSubscriptionChange
  14. );
  15. // External values could change between render and mount,
  16. // In some cases it may be important to handle this case.
  17. if (
  18. this.state.subscribedValue !==
  19. this.props.dataSource.value
  20. ) {
  21. this.setState({
  22. subscribedValue: this.props.dataSource.value,
  23. });
  24. }
  25. }
  26. componentWillUnmount() {
  27. this.props.dataSource.unsubscribe(
  28. this.handleSubscriptionChange
  29. );
  30. }
  31. handleSubscriptionChange = dataSource => {
  32. this.setState({
  33. subscribedValue: dataSource.value,
  34. });
  35. };
  36. }

adding-event-listeners-create-subscription

  1. import {createSubscription} from 'create-subscription';
  2. const Subscription = createSubscription({
  3. getCurrentValue(sourceProp) {
  4. // Return the current value of the subscription (sourceProp).
  5. return sourceProp.value;
  6. },
  7. subscribe(sourceProp, callback) {
  8. function handleSubscriptionChange() {
  9. callback(sourceProp.value);
  10. }
  11. // Subscribe (e.g. add an event listener) to the subscription (sourceProp).
  12. // Call callback(newValue) whenever a subscription changes.
  13. sourceProp.subscribe(handleSubscriptionChange);
  14. // Return an unsubscribe method.
  15. return function unsubscribe() {
  16. sourceProp.unsubscribe(handleSubscriptionChange);
  17. };
  18. },
  19. });
  20. // Rather than passing the subscribable source to our ExampleComponent,
  21. // We could just pass the subscribed value directly:
  22. <Subscription source={dataSource}>
  23. {value => <ExampleComponent subscribedValue={value} />}
  24. </Subscription>;