score:7

Accepted answer

the first issue can be solved with a null check before proceeding with your logic since react.createref() can also return null:

componentdidmount() {
  if(this.circleref !== null && this.circleref.current !== null) {
    this.circleref.current.setnativeprops({
      someproperty: somevalue
    });
  }
}

the second is solved by passing the class name of the node element for which you want to create a reference. for example, if your referenced element is a <text>, then do:

circleref = react.createref<text>();

this way, circleref will be correctly typed and setnativeprops will exist if and only if the referenced component is directly backed by a native view:

the methods [of current] are available on most of the default components provided by react native. note, however, that they are not available on composite components that aren't directly backed by a native view. this will generally include most components that you define in your own app. - direct manipulation - react native documentation

score:7

you can add typescript typings to a react native ref like this:

const textinputref: react.refobject<textinput> = react.createref();

Related Query

More Query from same tag