dimanche 11 octobre 2020

React - ref's current function fails on if statement

I am encountering a very wierd issue with React createRef

I call a ref and it only works in an else branch of an if/else statement.

if I remove the if statement, the ref does not work, and if the function goes to the IF path, it does not work too.

Here is a snippet.

sendFetch("/api/cases?q=" + JSON.stringify(query_payload), "", "GET").then(
  data => {
    this.fetchedData = data;
    console.log(data.count + " results found")
    //populate the table with results
    if (data.count === "0" || data.status === "failed") {
      console.log("NOT FOUND")
      this.tableRef.current.loadTablewithNewData(false)
      //NOT WORKING
    } else {
      //reload the table component with the new data
      this.tableRef.current.loadTablewithNewData(data.context)
      //WORKING
    }
  }
)

When the IF path is followed, or I remove the if/else alltogether and call the ref function right away, REACT shows this error × Unhandled Rejection (TypeError): Cannot read property 'loadTablewithNewData' of null

Aucun commentaire:

Enregistrer un commentaire