Skip to content

Commit c07cfc1

Browse files
authored
always do a second render for SSR compatible (#22)
1 parent 5b86838 commit c07cfc1

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/BaseTable.js

+23-20
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class BaseTable extends React.PureComponent {
5151

5252
const { columns, children, expandedRowKeys, defaultExpandedRowKeys } = props;
5353
this.state = {
54+
scrollbarSize: 0,
5455
hoveredRowKey: null,
5556
resizingKey: null,
5657
resizingWidth: 0,
@@ -96,7 +97,6 @@ class BaseTable extends React.PureComponent {
9697
this._horizontalScrollbarSize = 0;
9798
this._verticalScrollbarSize = 0;
9899
this._scrollbarPresenceChanged = false;
99-
this._scrollbarSizeMeasured = getScrollbarSize() !== undefined;
100100
}
101101

102102
/**
@@ -629,13 +629,10 @@ class BaseTable extends React.PureComponent {
629629
}
630630

631631
componentDidMount() {
632-
// in SSR getScrollbarSize() === undefined, so we have to measure again here
633-
if (!this._scrollbarSizeMeasured) {
634-
getScrollbarSize();
635-
this.setState({});
632+
const scrollbarSize = getScrollbarSize();
633+
if (scrollbarSize > 0) {
634+
this.setState({ scrollbarSize });
636635
}
637-
638-
this._maybeScrollbarPresenceChange();
639636
}
640637

641638
componentDidUpdate(prevProps, prevState) {
@@ -708,28 +705,34 @@ class BaseTable extends React.PureComponent {
708705

709706
_calcScrollbarSizes() {
710707
const { fixed, width } = this.props;
708+
const { scrollbarSize } = this.state;
709+
711710
const totalRowsHeight = this.getTotalRowsHeight();
712711
const totalColumnsWidth = this.getTotalColumnsWidth();
713-
const scrollbarSize = getScrollbarSize() || 0;
714712

715713
const prevHorizontalScrollbarSize = this._horizontalScrollbarSize;
716714
const prevVerticalScrollbarSize = this._verticalScrollbarSize;
717715

718-
// we have to set `this._horizontalScrollbarSize` before calling `this._getBodyHeight`
719-
if (!fixed || totalColumnsWidth <= width - scrollbarSize) {
716+
if (scrollbarSize === 0) {
720717
this._horizontalScrollbarSize = 0;
721-
this._verticalScrollbarSize = totalRowsHeight > this._getBodyHeight() ? scrollbarSize : 0;
718+
this._verticalScrollbarSize = 0;
722719
} else {
723-
if (totalColumnsWidth > width) {
724-
this._horizontalScrollbarSize = scrollbarSize;
725-
this._verticalScrollbarSize =
726-
totalRowsHeight > this._getBodyHeight() - this._horizontalScrollbarSize ? scrollbarSize : 0;
727-
} else {
720+
// we have to set `this._horizontalScrollbarSize` before calling `this._getBodyHeight`
721+
if (!fixed || totalColumnsWidth <= width - scrollbarSize) {
728722
this._horizontalScrollbarSize = 0;
729-
this._verticalScrollbarSize = 0;
730-
if (totalRowsHeight > this._getBodyHeight()) {
723+
this._verticalScrollbarSize = totalRowsHeight > this._getBodyHeight() ? scrollbarSize : 0;
724+
} else {
725+
if (totalColumnsWidth > width) {
731726
this._horizontalScrollbarSize = scrollbarSize;
732-
this._verticalScrollbarSize = scrollbarSize;
727+
this._verticalScrollbarSize =
728+
totalRowsHeight > this._getBodyHeight() - this._horizontalScrollbarSize ? scrollbarSize : 0;
729+
} else {
730+
this._horizontalScrollbarSize = 0;
731+
this._verticalScrollbarSize = 0;
732+
if (totalRowsHeight > this._getBodyHeight()) {
733+
this._horizontalScrollbarSize = scrollbarSize;
734+
this._verticalScrollbarSize = scrollbarSize;
735+
}
733736
}
734737
}
735738
}
@@ -748,7 +751,7 @@ class BaseTable extends React.PureComponent {
748751
this._scrollbarPresenceChanged = false;
749752

750753
onScrollbarPresenceChange({
751-
size: getScrollbarSize(),
754+
size: this.state.scrollbarSize,
752755
horizontal: this._horizontalScrollbarSize > 0,
753756
vertical: this._verticalScrollbarSize > 0,
754757
});

0 commit comments

Comments
 (0)