Skip to content

fix(useCookie): preserve empty cookie values - #2727

Open
feritcemkahraman wants to merge 1 commit into
streamich:masterfrom
feritcemkahraman:fix/cookie-empty-value
Open

feritcemkahraman wants to merge 1 commit into
streamich:masterfrom
feritcemkahraman:fix/cookie-empty-value

Conversation

@feritcemkahraman

@feritcemkahraman feritcemkahraman commented Oct 7, 2026 •

Copy link
Copy Markdown

Description

An existing cookie with an empty value is returned as null by useCookie, because initialization uses a truthiness fallback. Updating a cookie to an empty string works until the component is remounted, when the same persisted value becomes null.

Use a nullish fallback so only a missing cookie maps to null. Add regression tests for an existing empty cookie and an empty update followed by remount, and document the empty-string/missing-cookie distinction.

This addresses value initialization independently of the cookie-name-change behavior proposed in #2723.

Validation

  • Jest 26 with React 17.0.2 and js-cookie 3.0.5: both new regressions fail before the fix; all six useCookie tests pass after it.
  • Targeted TypeScript 4.1.5 strict check of the changed hook passes.
  • Targeted coverage: 100% statements, functions and lines; 90.9% branches (including transpiler-generated branches).
  • Full repository npm test now passes: 76 suites and 494 tests, using the original Jest configuration.
  • Full repository npm run lint passes with 0 errors and 97 existing warnings after normalizing the local Windows checkout to LF; no source changes were introduced.
  • Full npm run lint:types and npm run build:cjs were attempted. They fail in dependency declarations: initially newer @types/babel__traverse syntax unsupported by TypeScript 4.1.5; using its yarn.lock version (7.11.0) exposes three nano-css/csstype declaration errors. No package or lockfile changes are included.
  • git diff --check passes.

Type of change

  • Bug fix

Checklist

  • Read the Contributing Guide
  • Perform a code self-review
  • Comment the code, particularly in hard-to-understand areas
  • Add documentation
  • Add hook's story at Storybook
  • Cover changes with tests
  • Ensure the test suite passes (yarn test)
  • Provide 100% tests coverage
  • Make sure code lints (yarn lint).
  • Make sure types are fine (yarn lint:types).

ESLint 8 using the repository's configuration passes on both changed TypeScript files (via the isolated dependency harness).

@feritcemkahraman
feritcemkahraman marked this pull request as ready for review October 8, 2026 01:46

This branch has not been deployed

No deployments
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