javascriptiosreact-nativelistenerios8-today-widget

React Native: This is not a function handling custom URL scheme


I have followed a tutorial on how to handle a custom URL scheme. This is the way I have it set up.

componentDidMount() {
    Linking.addEventListener('url', this.handleOpenURL);
  }

componentWillUnmount() {
   Linking.removeEventListener('url', this.handleOpenURL);
  }

handleOpenURL(event) {
   console.log(event.url);
   this.abc()
  }

 abc() {
   console.log("Hello World");
 }

handleOpenUrl function is being called, but function abc isn't. I click on a today widget button which opens my app with a custom URL from background to foreground. I am getting the error message "this.abc is not a function" on my iPhone simulator. I am new to react native and not sure why this is. I think maybe the script hasn't loaded or something when I go from background to foreground in my app.


Solution

  • You have to bind handleOpenURL to your component.

    Replace

    handleOpenURL(event) {
       console.log(event.url);
       this.abc()
    }
    

    with

    handleOpenURL = (event) => {
       console.log(event.url);
       this.abc()
    }