T
TanStack9mo ago
quickest-silver

Possible to update state via functionalUpdate by passing in updater manually?

I have a state like this.
// Model
pageTokens: Record<number, string | undefined>;

// Setter
table.setPageToken = (page, token) => {
table.setState((old) => {
return {
...old,
pageTokens: {
...old.pageTokens,
[page]: token,
},
};
});
};
// Model
pageTokens: Record<number, string | undefined>;

// Setter
table.setPageToken = (page, token) => {
table.setState((old) => {
return {
...old,
pageTokens: {
...old.pageTokens,
[page]: token,
},
};
});
};
Trying to update this state within the pagination onChange. I do not have access to the table instance here.
onPaginationChange: (updater) => {
const currentPagination = {
pageIndex: search.page - 1, // Set to 0-based index
pageSize: search.pageSize,
};

const newPagination = functionalUpdate(updater, currentPagination);

const newPage = newPagination.pageIndex + 1; // Set back to 1-based index;
const newPageSize = newPagination.pageSize;

functionalUpdate(
(old) => {
return {
...old,
pageTokens: {
...old.pageTokens,
[newPage]: search.token,
},
};
},
{ pageTokens: { [newPage]: search.token } },
);
onPaginationChange: (updater) => {
const currentPagination = {
pageIndex: search.page - 1, // Set to 0-based index
pageSize: search.pageSize,
};

const newPagination = functionalUpdate(updater, currentPagination);

const newPage = newPagination.pageIndex + 1; // Set back to 1-based index;
const newPageSize = newPagination.pageSize;

functionalUpdate(
(old) => {
return {
...old,
pageTokens: {
...old.pageTokens,
[newPage]: search.token,
},
};
},
{ pageTokens: { [newPage]: search.token } },
);
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?