Say you have a new user, Joe Bloggs. It would look like this (note that the setter is setTableState — the setter you got from useState — not setState):
setTableState(prevState => ({
...prevState,
data: [
...prevState.data, {
firstName: "Joe",
lastName: "Bloggs"
}
]
}));
What that does:
Creates a new object which is a shallow copy of prevState, but
With a new data property, that
Has the original data, plus the new object
But stepping back a bit: That state item is probably too complicated. With hooks and useState, you usually want to keep your state really finely-grained. In this case, I think there are at least two separate state items:
const [columns, setColumns] = useState<ColumnType[]>([
{ title: "First Name", field: "firstName" },
{ title: "Last Name", field: "lastName" },
]);
const [data, setData] = useState<DataType[]>([
{
firstName: "John",
lastName: "Doe",
},
{
firstName: "Jane",
lastName: "Doe",
},
]);
And you'd set new data state like this:
setData(data => [
...data,
{
firstName: "Joe",
lastName: "Bloggs"
}
]);