-
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Delay collection which makes scroll not working (#650)
* chore: useUpdate for in frame * fix: limitation check * chore: clean up * chore: not care scroll * chore: add additional check * fix: ping logic
- Loading branch information
Showing
5 changed files
with
78 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useLayoutUpdateEffect } from 'rc-util/lib/hooks/useLayoutEffect'; | ||
import { useRef, useState } from 'react'; | ||
|
||
/** | ||
* Help to merge callback with `useLayoutEffect`. | ||
* One time will only trigger once. | ||
*/ | ||
export default function useUpdate(callback: VoidFunction): () => void { | ||
const [count, setCount] = useState(0); | ||
const effectRef = useRef(0); | ||
const callbackRef = useRef<VoidFunction>(); | ||
callbackRef.current = callback; | ||
|
||
// Trigger on `useLayoutEffect` | ||
useLayoutUpdateEffect(() => { | ||
callbackRef.current?.(); | ||
}, [count]); | ||
|
||
// Trigger to update count | ||
return () => { | ||
if (effectRef.current !== count) { | ||
return; | ||
} | ||
|
||
effectRef.current += 1; | ||
setCount(effectRef.current); | ||
}; | ||
} | ||
|
||
type Callback<T> = (ori: T) => T; | ||
|
||
export function useUpdateState<T>( | ||
defaultState: T | (() => T), | ||
): [T, (updater: Callback<T>) => void] { | ||
const batchRef = useRef<Callback<T>[]>([]); | ||
const [, forceUpdate] = useState({}); | ||
const state = useRef<T>( | ||
typeof defaultState === 'function' ? (defaultState as any)() : defaultState, | ||
); | ||
|
||
const flushUpdate = useUpdate(() => { | ||
let current = state.current; | ||
batchRef.current.forEach(callback => { | ||
current = callback(current); | ||
}); | ||
batchRef.current = []; | ||
|
||
state.current = current; | ||
forceUpdate({}); | ||
}); | ||
|
||
function updater(callback: Callback<T>) { | ||
batchRef.current.push(callback); | ||
flushUpdate(); | ||
} | ||
|
||
return [state.current, updater]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89d01ff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
tabs – ./
tabs-react-component.vercel.app
tabs-git-master-react-component.vercel.app