Replies: 4 comments 9 replies
-
Observe change in state through useEffect
…On Fri, Jul 3, 2020, 4:13 AM szmarci ***@***.***> wrote:
I want to save column widths to localstorage, but for that I would need an
onDragEnd event to the resizer.
I tried to add it myself like this:
<div
{...column.getResizerProps()}
className={`resizer ${column.isResizing ? 'isResizing' : ''}`}
onDragEnd={}
/>
But it didn't not work. I also tried onDragEndCapture with the same
result.
How can I catch that moment when resize ended?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2498>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKEG7NYU4B75AUC5J53J6YDRZUERVANCNFSM4OPJQIVA>
.
|
Beta Was this translation helpful? Give feedback.
-
Related to this discussion: what is the recommended way to implement a reset function that will revert the column sizes to their original values? only passing the columns with new witsh won't work, because react-table still renders based upon its internal state. |
Beta Was this translation helpful? Give feedback.
-
I implemented a import * as React from "react";
export interface ExtendedTableState {
/**
* Column resize information. This will only have a value if ReactTable.useResizeColumns is being used
*/
columnResizing?: ColumnResizing;
/**
* An array with the columns IDs in order. This will only have a value if ReactTable.useColumnOrder is being used
*/
columnOrder?: string[];
hiddenColumns?: string[] | null;
}
export interface ColumnResizing {
/**
* The default column width. This doesn't change.
*/
columnWidth: number | null;
/**
* Map of column IDs to column widths.
*/
columnWidths: Record<string, number>;
startX: number;
/**
* I don't know why this exists. And I don't know why header widths vary from the column widths.
*/
headerIdWidths?: [string, number][];
/**
* The ID of the column being resized. Will be null if no column is being resized.
* This is a bit of misleading name because "is" imply it's a boolean but it is not.
*/
isResizingColumn?: string;
}
export const useResizeObserver = (
state: ExtendedTableState,
callback: (columnId: string, columnSize: number) => void
) => {
// This Ref will contain the id of the column being resized or undefined
const columnResizeRef = React.useRef<string | undefined>();
React.useEffect(() => {
// We are interested in calling the resize event only when "state.columnResizing?.isResizingColumn" changes from
// a string to undefined, because it indicates that it WAS resizing but it no longer is.
if (
state.columnResizing &&
!state.columnResizing?.isResizingColumn &&
columnResizeRef.current
) {
// Trigger resize event
callback(
columnResizeRef.current,
state.columnResizing.columnWidths[columnResizeRef.current]
);
}
columnResizeRef.current = state.columnResizing?.isResizingColumn;
}, [state.columnResizing?.isResizingColumn]);
}; Example of usage: const {
headerGroups,
rows,
prepareRow,
setColumnOrder,
visibleColumns,
state,
} = ReactTable.useTable(
{ columns, data: mergedData, defaultColumn } as any,
ReactTable.useColumnOrder,
ReactTable.useResizeColumns
);
useResizeObserver(state as ExtendedTableState, (columnId, columnSize) => {
console.log("resized");
console.log(columnId);
console.log(columnSize);
}); |
Beta Was this translation helpful? Give feedback.
-
Updated for latest version: import { TableState } from "@tanstack/table-core";
import * as React from "react";
export const useResizeObserver = (
state: TableState,
callback: (columnId: string, columnSize: number) => void
) => {
// This Ref will contain the id of the column being resized or undefined
const columnResizeRef = React.useRef<string | false>();
React.useEffect(() => {
// We are interested in calling the resize event only when "state.columnResizingInfo?.isResizingColumn" changes from
// a string to false, because it indicates that it WAS resizing but it no longer is.
if (
state.columnSizingInfo &&
!state.columnSizingInfo?.isResizingColumn &&
columnResizeRef.current
) {
// Trigger resize event
callback(
columnResizeRef.current,
state.columnSizing[columnResizeRef.current]
);
}
columnResizeRef.current = state.columnSizingInfo?.isResizingColumn;
}, [callback, state.columnSizingInfo, state.columnSizing]);
}; |
Beta Was this translation helpful? Give feedback.
-
I want to save column widths to localstorage, but for that I would need an
onDragEnd
event to the resizer.I tried to add it myself like this:
But it didn't not work. I also tried
onDragEndCapture
with the same result.How can I catch that moment when resize ended?
Beta Was this translation helpful? Give feedback.
All reactions