I have a table in Next.js that, despite inputting the text into a text field, isn't being redrawn. When redrawn, the table should return a single result, but it is currently only updating the top row to make the search result visible.
Here is my useEffect
and where my state is declared:
const [filteredData, setFilteredData] = useState([]);
const [inputText, setInputText] = useState("");
//Get data from database
useEffect(() => {
if (!checked) {
setChecked(true);
fetchData();
}
let data: any =
inputText && inputText !== ""
? rows.filter(
(item: any) =>
item?.product_id?.toLowerCase().indexOf(inputText.toLowerCase()) !==
-1
)
: rows;
setFilteredData(data);
console.log("results: ", data, inputText);
}, [checked, inputText]);
Here is my fetchData()
function:
const fetchData: any = async () => {
const response: any = await axios.get(`/api/layer_data`);
setChecked(true);
// setRows.checked(false);
let dataRows: any[] = response.data;
dataRows.map((dataRow: any) => (dataRow.isSelected = false));
console.log("response: ", response.data);
setRows(dataRows);
setFilteredData(dataRows as any);
};
Here is where I draw the table:
<>
{filteredData
?.sort(getComparator(order, orderBy))
.map((row: any, index: any) => {
const isItemSelected = isSelected(row.product_id);
const labelId = `enhanced-table-checkbox-${index}`;
return (
<StyledTableRow
hover
onClick={(event) => handleClick(event, row.product_id)}
role="checkbox"
aria-checked={isItemSelected}
tabIndex={-1}
key={row.product_id}
selected={isItemSelected}
>
<StyledTableCell padding="checkbox">
<Checkbox
color="primary"
checked={row.isSelected}
inputProps={{
"aria-labelledby": labelId,
}}
onChange={handleCheckbox}
value={index}
/>
</StyledTableCell>
<StyledTableCell align="right">
<input
type="number"
min="0"
required
defaultValue="0"
onChange={(e) => handleInput(e, index)}
/>
</StyledTableCell>
<StyledTableCell align="right">{row.sku_id}</StyledTableCell>
<StyledTableCell
component="th"
id={labelId}
scope="row"
padding="none"
align="right"
>
{row.product_id}
</StyledTableCell>
<StyledTableCell align="right">{row.in_stock}</StyledTableCell>
<StyledTableCell align="right">{row.bin}</StyledTableCell>
<StyledTableCell align="right">{row.units_per_layer}</StyledTableCell>
<StyledTableCell align="right">{row.description}</StyledTableCell>
</StyledTableRow>
);
})}
</>;
Any help is greatly appreciated. I appreciate any help you can provide.