'Rows not defined when it is defined inside if-else block for table data filter

I am doing filtration on my data on multiple conditions using if-else. The if is for when a person clicks advance search and then we have to filter based on certain fields and the else is based on the regular search field and if that is empty, the whole table data is shown. The logic should be alright but when compiling, React throws me an error

'rows' is not defined no-undef

Is it because it is inside the if-else and should be declared outside? I tried declaring a null rows const but if I do that, it never goes into the if-else and shows me 0 records. What should I change for this to work?

relevant code:


  if (advanceSearch == true) {
    const rows = data.filter((item) => {
      return (
        item.doc_id.toString().match(new RegExp(searchDocID, "gi")) ||
        item.invoice_id.toString().match(new RegExp(searchInvoiceID, "gi")) ||
        item.cust_number.toString().match(new RegExp(searchCustNum, "gi")) ||
        item.buisness_year.toString().match(new RegExp(searchBizzYear, "gi"))
      )
    });
  } else {
    const rows = searchInput
      ? data.filter((item) => item.cust_number.toString().match(new RegExp("^" + searchInput, "gi")))
      : data;
  }
  

    return (
      <div style={{ width: '100%' }}>
        <DataGrid
            rows={rows}
            columns={columns}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source