philipphecht / react-native-doc-viewer

React Native Doc Viewer (Supports file formats: xls,ppt,doc,xlsx,pptx,csv,docx,png,jpg,pdf,xml,binary ...)
MIT License
94 stars 116 forks source link

Sending `DoneButtonEvent` with no listeners registered. #123

Closed qiaolin-pan closed 5 years ago

qiaolin-pan commented 5 years ago

When I click the Done button, I get the following error: Sending 'DoneButtonEvent' with no listeners registered.

gwdp commented 5 years ago

+1

wenjianli92 commented 5 years ago

+1,Is there a solution?

wutiange commented 4 years ago

YellowBox.js:67 Sending RNDownloaderProgress with no listeners registered. Sending DoneButtonEvent with no listeners registered.

I have these two warnings

koktavy commented 4 years ago

The package documentation should be updated regarding the best practices to handle these events. I added these lines to my component (along with relevant imports) to satisfy this warning:

  private eventEmitter: EventEmitter = new NativeEventEmitter(NativeModules.RNReactNativeDocViewer);

  componentDidMount = () => {
    // Prevent warnings by listening to the DoneButtonEvent that is emitted when closing the iOS QuickLook preview
    this.eventEmitter.addListener('DoneButtonEvent', this.doneEvent);
  };

  componentWillUnmount = () => {
    this.eventEmitter.removeListener('DoneButtonEvent', this.doneEvent);
  };

  doneEvent = () => {
    console.log('Closing QuickLook viewer');
  };

Edit: expanded to include named function and properly remove listeners.

prasad456 commented 3 years ago

how to use this eventemitter in hooks component?

atha-github commented 1 year ago

For Functional components use this:

// Function to handle the DoneButtonEvent
  const doneEvent = () => {
    console.log('Closing QuickLook viewer');
  };

  useEffect(() => {
    // Create an instance of the NativeEventEmitter and associate it with the RNReactNativeDocViewer native module
    const eventEmitter = new NativeEventEmitter(NativeModules.RNReactNativeDocViewer);

    // Add the event listener for the DoneButtonEvent when the component mounts
    const doneButtonListener = eventEmitter.addListener('DoneButtonEvent', doneEvent);

    // Clean up: Remove the event listener when the component unmounts
    return () => {
      doneButtonListener.remove();
    };
  }, []);