Hi All
Very new to react, trying hooks to simplify code and readability.
I am trying to implement the odata service example from: https://www.telerik.com/kendo-react-ui/components/grid/data-operations/odata-server-operations/
The odata service is fine, I am just trying to get the function setGridState1 to set the grid state to gridState and then call the function toODataString passing in the grid state (and therefore parsing it to an odata string) and then passing it to my rest api.
I can get the debugger points to trigger, but after I update a filter in the grid and it calls fetchData, gridState is still null?
Missing something crucial here, not sure what it is though, any help would be great.
import * as React from
"react"
;
import { Grid, GridColumn as Column } from
"@progress/kendo-react-grid"
;
import { toODataString } from
'@progress/kendo-data-query'
;
import { dataService } from
"../Services/dataService"
;
export const AddExistingGrid: React.FC = () => {
const [selectedID, setSelectedID] = React.useState(
null
)
const [editId, setEditId] = React.useState(undefined);
const [data, setData] = React.useState<any[]>([]);
const [gridState, setGridState] = React.useState<any>([]);
const fetchData = React.useCallback(async () => {
debugger;
const newData = await dataService.getLoanInvOdata(toODataString(gridState));
}, [setData]);
const setGridState1 = (e) => {
setGridState(e);
fetchData();
debugger;
}
React.useEffect(() => { fetchData() }, [fetchData]);
return
(
<>
<div>
<Grid
filterable={
true
}
sortable={
true
}
pageable={
true
}
data={data.map(item => ({
...item,
inEdit: item.ProductID === editId,
selected: item.ProductID === selectedID
}))}
onDataStateChange={setGridState1}
>
<Column field=
"id"
title=
"Id"
/>
<Column field=
"ProductName"
title=
"Name"
/>
<Column
field=
"UnitPrice"
filter=
"numeric"
format=
"{0:c}"
title=
"Price"
/>
<Column field=
"UnitsInStock"
filter=
"numeric"
title=
"In stock"
/>
</Grid>
</div>
</>
);
};