Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initializeWithValue option to useCookie hook #120

Merged
merged 1 commit into from
Jun 14, 2021

Conversation

xobotyi
Copy link
Contributor

@xobotyi xobotyi commented Jun 14, 2021

BREAKING CHANGE: useCookie renamed to useCookieValue

BREAKING CHANGE: useCookieValue default behaviour for browsers
changed to fetch cookie value on state initialisation.

SSR remains untouched, but requires implicit setting of
initializeWithValue option to false, to avoid hydration mismatch.

What is the current behavior, and the steps to reproduce the issue?

Hook fetches cookie value onli on effects execution stage, thus first render always yields undefined state.

What is the expected behavior?

Ability to enable or disable initialisation fetch.

How does this PR fix the problem?

Add initializeWithValue option to disable initialisation value fetch and make hook to fetch value on initialisation by default.

Checklist

  • Have you read contribution guideline?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Is there an existing issue for this PR?
    • link issue here
  • Have the files been linted and formatted?
  • Have the docs been updated to match the changes in the PR?
  • Have the tests been updated to match the changes in the PR?
  • Have you run the tests locally to confirm they pass?

@xobotyi xobotyi added the ✨ enhancement New feature or request label Jun 14, 2021
@xobotyi xobotyi requested a review from JoeDuncko June 14, 2021 07:43
@codecov
Copy link

codecov bot commented Jun 14, 2021

Codecov Report

Merging #120 (3c3be12) into master (8077e6e) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #120   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           31        31           
  Lines          502       514   +12     
  Branches        87        91    +4     
=========================================
+ Hits           502       514   +12     
Impacted Files Coverage Δ
src/useStorageValue/useStorageValue.ts 100.00% <ø> (ø)
src/index.ts 100.00% <100.00%> (ø)
src/useCookieValue/useCookieValue.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8077e6e...3c3be12. Read the comment docs.

- **options** _`IUseCookieOptions`_ _(default: {})_ - Cookie options that will be
used during cookie set and delete. Has only one extra option, that relates to the hook itself:
- **initializeWithValue** _`boolean`_ _(default: undefined)_ - Whether to initialize state with
cookie value or initialize with `undefined` state.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the default be true, not undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In code it is undefined =)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... that doesn't really line up with our other docs for hooks with storeDefaultValue though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where?

While using SSR, to avoid hydration mismatch, consider setting initializeWithValue option
to false, this will yield undefined state on first render and defer value fetch till effects
execution stage.

Despite from that - maybe it is slightly confusing, as false and undefined both perceived as falsy values.

Copy link
Contributor

@JoeDuncko JoeDuncko Jun 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watched the code - it defaults to true %)
smth with my memory today 😑

Copy link
Contributor

@JoeDuncko JoeDuncko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, besides the typo above

@JoeDuncko
Copy link
Contributor

Question! Why is this hook called useCookie and not useCookieValue?

@xobotyi
Copy link
Contributor Author

xobotyi commented Jun 14, 2021

I had similar thoughts, but kept such naming to have consistency with useLocalStorage useSessionStorage

@JoeDuncko
Copy link
Contributor

Consistency with which implementations of useLocalStorage and useSessionStorage? Ours are called useLocalStorageValue anduseSessionStorageValue right? Maybe I'm a bit confused.

@xobotyi
Copy link
Contributor Author

xobotyi commented Jun 14, 2021

LMAO, i've got confused, forgot that ive renamed it 🙃
I'lll do renaming in this PR to avoid useless major versions bump

BREAKING CHANGE: `useCookie` renamed to `useCookieValue`

BREAKING CHANGE: `useCookieValue` default behaviour for browsers
changed to fetch cookie value on state initialisation.

SSR remains untouched, but requires implicit setting of
`initializeWithValue` option to false, to avoid hydration mismatch.
@xobotyi xobotyi force-pushed the improve-useCookie branch from f94a1be to 3c3be12 Compare June 14, 2021 17:12
@xobotyi
Copy link
Contributor Author

xobotyi commented Jun 14, 2021

@JoeDuncko i've also done renaming, re-review briefly please.

Copy link
Contributor

@JoeDuncko JoeDuncko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@xobotyi xobotyi merged commit 17c9543 into master Jun 14, 2021
@xobotyi xobotyi deleted the improve-useCookie branch June 14, 2021 17:28
github-actions bot pushed a commit that referenced this pull request Jun 14, 2021
# [2.0.0](v1.28.0...v2.0.0) (2021-06-14)

### Features

* add `initializeWithValue` option to `useCookie` hook ([#120](#120)) ([17c9543](17c9543))

### BREAKING CHANGES

* `useCookie` renamed to `useCookieValue`
* `useCookieValue` default behaviour for browsers
changed to fetch cookie value on state initialisation.

SSR remains untouched, but requires implicit setting of
`initializeWithValue` option to false, to avoid hydration mismatch.
@xobotyi
Copy link
Contributor Author

xobotyi commented Jun 14, 2021

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request released
Development

Successfully merging this pull request may close these issues.

2 participants