Skip to content
This repository was archived by the owner on Nov 4, 2025. It is now read-only.

Commit ff33248

Browse files
committed
fix: Use ref to save props
1 parent d11717b commit ff33248

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

examples/simple.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ class Test extends Component {
1616
this.id = setInterval(() => {
1717
const { random } = this.state;
1818
if (random) {
19-
this.setState(
20-
{
21-
randomWidth: 60 + 40 * Math.random(),
22-
},
23-
() => {
24-
this.forceAlign();
25-
},
26-
);
19+
this.setState({
20+
randomWidth: 60 + 40 * Math.random(),
21+
});
2722
}
2823
}, 1000);
2924
}
@@ -101,8 +96,8 @@ class Test extends Component {
10196
ref={this.containerRef}
10297
id="container"
10398
style={{
104-
// width: '80%',
105-
maxWidth: 1000,
99+
width: '80%',
100+
// maxWidth: 1000,
106101
height: 500,
107102
border: '1px solid red',
108103
...(random

src/Align.tsx

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import { isSamePoint, restoreFocus, monitorResize } from './util';
1313
import { AlignType, AlignResult, TargetType, TargetPoint } from './interface';
1414
import useBuffer from './hooks/useBuffer';
1515

16+
type OnAlign = (source: HTMLElement, result: AlignResult) => void;
17+
1618
export interface AlignProps {
1719
align: AlignType;
1820
target: TargetType;
19-
onAlign?: (source: HTMLElement, result: AlignResult) => void;
21+
onAlign?: OnAlign;
2022
monitorBufferTime?: number;
2123
monitorWindowResize?: boolean;
2224
disabled?: boolean;
@@ -51,19 +53,24 @@ const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
5153
let childNode = React.Children.only(children);
5254

5355
// ===================== Align ======================
54-
const forceAlignRef = React.useRef<Function>();
56+
// We save the props here to avoid closure makes props ood
57+
const forceAlignPropsRef = React.useRef<{
58+
disabled?: boolean;
59+
target?: TargetType;
60+
onAlign?: OnAlign;
61+
}>({});
62+
forceAlignPropsRef.current.disabled = disabled;
63+
forceAlignPropsRef.current.target = target;
64+
forceAlignPropsRef.current.onAlign = onAlign;
5565

5666
const [forceAlign, cancelForceAlign] = useBuffer(() => {
57-
forceAlignRef.current();
58-
}, monitorBufferTime);
59-
60-
forceAlignRef.current = () => {
61-
if (!disabled && target) {
67+
const { disabled: latestDisabled, target: latestTarget } = forceAlignPropsRef.current;
68+
if (!latestDisabled && latestTarget) {
6269
const source = findDOMNode<HTMLElement>(nodeRef.current);
6370

6471
let result: AlignResult;
65-
const element = getElement(target);
66-
const point = getPoint(target);
72+
const element = getElement(latestTarget);
73+
const point = getPoint(latestTarget);
6774

6875
cacheRef.current.element = element;
6976
cacheRef.current.point = point;
@@ -83,10 +90,12 @@ const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
8390
if (onAlign) {
8491
onAlign(source, result);
8592
}
86-
} else {
87-
cancelForceAlign();
93+
94+
return true;
8895
}
89-
};
96+
97+
return false;
98+
}, monitorBufferTime);
9099

91100
// ===================== Effect =====================
92101
// Listen for target updated

src/hooks/useBuffer.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
export default (callback: Function, buffer: number) => {
3+
export default (callback: () => boolean, buffer: number) => {
44
const calledRef = React.useRef<boolean>(false);
55
const timeoutRef = React.useRef<number>(null);
66

@@ -10,9 +10,12 @@ export default (callback: Function, buffer: number) => {
1010

1111
function trigger() {
1212
if (!calledRef.current) {
13-
callback();
14-
calledRef.current = true;
13+
if (callback() === false) {
14+
// Not delay since callback cancelled self
15+
return;
16+
}
1517

18+
calledRef.current = true;
1619
cancelTrigger();
1720
timeoutRef.current = window.setTimeout(() => {
1821
calledRef.current = false;

0 commit comments

Comments
 (0)