React native creating reference is undefined

Viewed 19

I am pretty new to react and realize that this is a dummy question, but I don't know what am I doing wrong

Basically what I want is to create reference to WebView and what I have is this

import React, { Component, createRef } from "react"

...

class NotificationDetailsScreen extends Component<IProps, IState> {
    deviceOrientationChange: any
    WEBVIEW_REF: React.RefObject<WebView> = createRef()
...

   constructor(props) {
        super(props)
        this.WEBVIEW_REF = createRef()
...

    onSwipeLeft() {
     console.log("event trigger")
     this.WEBVIEW_REF.current.injectJavaScript('alert("hello")')  
    }

...

 <WebView ref={webview => this.WEBVIEW_REF = webview}
          style={styles.webview}
          originWhitelist={['*']}
          source={{ html: "<html><body>"+fullDescription ?? '' + "</body></html>"}}/>

The function onSwipeLeft is been called, but I have the following error:

ERROR TypeError: undefined is not an object (evaluating 'this.WEBVIEW_REF.current.injectJavaScript')

I can't figure out what am I missing

0 Answers
Related