Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,7 @@ function Component() {
## 6.1.8(Dec 2025)

- fix(useMap): fix type parameter support by moving generics into function signature, now `useMap<string, number>()` works correctly

## 6.1.9(Jan 2026)

- fix(useRafState): fix bug where multiple consecutive functional updates would only apply the last one. Now correctly accumulates all updates within the same animation frame, matching React's useState behavior. For example, calling `setState(n => n + 1)` three times consecutively will now correctly increase the value by 3 instead of 1.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reactuses/core",
"version": "6.1.8",
"version": "6.1.9",
"license": "Unlicense",
"homepage": "https://www.reactuse.com/",
"repository": {
Expand Down
45 changes: 45 additions & 0 deletions packages/core/src/useRafState/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,49 @@ describe('useRafState', () => {
expect(result.current[0]).toBe(1)
mockRaf.mockRestore()
})

it('should handle multiple consecutive functional updates correctly', () => {
const mockRaf = jest
.spyOn(window, 'requestAnimationFrame')
.mockImplementation((cb: FrameRequestCallback) => {
cb(0)
return 0
})
const { result } = renderHook(() => useRafState(0))
const setRafState = result.current[1]
expect(result.current[0]).toBe(0)

// Multiple consecutive updates in the same tick
act(() => {
setRafState(count => count + 1)
setRafState(count => count + 1)
setRafState(count => count + 1)
})

// Should apply all three updates, resulting in 3
expect(result.current[0]).toBe(3)
mockRaf.mockRestore()
})

it('should handle mixed value and functional updates', () => {
const mockRaf = jest
.spyOn(window, 'requestAnimationFrame')
.mockImplementation((cb: FrameRequestCallback) => {
cb(0)
return 0
})
const { result } = renderHook(() => useRafState(0))
const setRafState = result.current[1]
expect(result.current[0]).toBe(0)

act(() => {
setRafState(5)
setRafState(count => count + 1)
setRafState(count => count * 2)
})

// Should apply: set to 5, then +1 (=6), then *2 (=12)
expect(result.current[0]).toBe(12)
mockRaf.mockRestore()
})
})
11 changes: 10 additions & 1 deletion packages/core/src/useRafState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import { useUnmount } from '../useUnmount'
export function useRafState<S>(initialState: S | (() => S)): readonly [S, Dispatch<SetStateAction<S>>] {
const frame = useRef(0)
const [state, setState] = useState(initialState)
const pendingUpdates = useRef<Array<SetStateAction<S>>>([])

const setRafState = useCallback((value: S | ((prevState: S) => S)) => {
pendingUpdates.current.push(value)
cancelAnimationFrame(frame.current)

frame.current = requestAnimationFrame(() => {
setState(value)
const updates = pendingUpdates.current.splice(0)
setState(prevState => {
let newState = prevState
for (const update of updates) {
newState = typeof update === 'function' ? (update as (prevState: S) => S)(newState) : update
}
return newState
})
})
}, [])

Expand Down
4 changes: 4 additions & 0 deletions packages/website-docusaurus/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,7 @@ function Component() {
## 6.1.8(Dec 2025)

- fix(useMap): fix type parameter support by moving generics into function signature, now `useMap<string, number>()` works correctly

## 6.1.9(Jan 2026)

- fix(useRafState): fix bug where multiple consecutive functional updates would only apply the last one. Now correctly accumulates all updates within the same animation frame, matching React's useState behavior. For example, calling `setState(n => n + 1)` three times consecutively will now correctly increase the value by 3 instead of 1.