Skip to content

fix(useStateWithHistory): update position after trimming history - #2730

Closed
Aminark wants to merge 2 commits into
streamich:masterfrom
Aminark:codex/history-capacity-position
Closed

Aminark wants to merge 2 commits into
streamich:masterfrom
Aminark:codex/history-capacity-position

Conversation

@Aminark

@Aminark Aminark commented Oct 23, 2026

Copy link
Copy Markdown

Description

When useStateWithHistory reaches its capacity, position is calculated before the oldest entries are removed. It can point beyond the retained history: with capacity 3, setting 0 β†’ 1 β†’ 2 β†’ 3 leaves history [1, 2, 3] at position 3, so the first back() stays on 3 instead of returning 2. Reducing capacity can make back() return undefined.

Calculate the position after trimming, so it points to the newly added entry in the retained history. Three regression tests cover repeated overflow with back/forward navigation, capacity 1, and reducing capacity before the next update. All three fail against the original implementation.

Type of change

  • Bug fix (non-breaking)

Validation

  • yarn test --coverage --collectCoverageFrom=src/useStateWithHistory.ts: 76 suites, 495 tests passed; the changed hook has 100% statement, branch, function and line coverage.
  • yarn lint: passed with 0 errors and 97 existing warnings.
  • yarn lint:types: passed.
  • yarn build: CommonJS and ES module builds passed.
  • git diff --check: passed.

Checklist

  • Read the contributing guide.
  • Reviewed the implementation and tests.
  • Added failing-before/passing-after regression tests.
  • Full tests, lint, types and builds pass.
  • Changed hook has 100% test coverage.
  • Considered documentation and Storybook: no public API or usage changes.

AI assistance: OpenAI Codex prepared the implementation and tests, ran validation, and drafted this PR.

@Aminark Aminark closed this Oct 23, 2026
@Aminark
Aminark deleted the codex/history-capacity-position branch October 23, 2026 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant