-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
11 changed files
with
164 additions
and
93 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.
4 changes: 2 additions & 2 deletions
4
src/useCookie/__docs__/example.stories.tsx → ...eCookieValue/__docs__/example.stories.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; | ||
import { Example } from './example.stories'; | ||
|
||
<Meta title="Side-effect/useCookieValue" component={Example} /> | ||
|
||
# useCookieValue | ||
|
||
Manages a single cookie. | ||
|
||
> Requires installation of `js-cookie` package. | ||
- Uses `js-cookie` package underneath. | ||
- SSR-friendly. | ||
- Hooks that managing same key on same page - are synchronised. This synchronisation does not | ||
involve cross-tabs sync or triggering on changes that performed by third-party code. | ||
|
||
> **_This hook provides stable API, meaning returned methods does not change between renders_** | ||
> Uses `null` values as indicator of cookie absence, `undefined` value means that cookie value | ||
> hasn't been fetched yet. | ||
> 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. | ||
#### Example | ||
|
||
<Canvas columns={3}> | ||
<Story story={Example} inline /> | ||
<Story story={Example} inline /> | ||
<Story story={Example} inline /> | ||
</Canvas> | ||
|
||
## Reference | ||
|
||
```ts | ||
export type IUseCookieOptions = Cookies.CookieAttributes & { | ||
initializeWithValue?: boolean; | ||
}; | ||
|
||
export type IUseCookieReturn = [ | ||
value: undefined | null | string, | ||
set: (value: string) => void, | ||
remove: () => void, | ||
fetch: () => void | ||
]; | ||
|
||
export function useCookieValue(key: string, options: IUseCookieOptions = {}): IUseCookieReturn; | ||
``` | ||
|
||
#### Arguments | ||
|
||
- **key** _`string`_ - Cookie name to manage. | ||
- **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: true)_ - Whether to initialize state with | ||
cookie value or initialize with `undefined` state. | ||
_Default to `false` during SSR._ | ||
|
||
#### Return | ||
|
||
0. **state** - cookie value, `undefined` means it is not fetched yet, `null` means absence of | ||
cookie. | ||
1. **set** - Method to set new cookie value. | ||
2. **remove** - Method to remove cookie. | ||
3. **fetch** - Method to re-fetch cookie value. |
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
10 changes: 5 additions & 5 deletions
10
src/useCookie/__tests__/ssr.tsx → src/useCookieValue/__tests__/ssr.tsx
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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useCookie } from '../..'; | ||
import { useCookieValue } from '../..'; | ||
|
||
describe('useCookie', () => { | ||
describe('useCookieValue', () => { | ||
it('should be defined', () => { | ||
expect(useCookie).toBeDefined(); | ||
expect(useCookieValue).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useCookie('react-hookz')); | ||
const { result } = renderHook(() => useCookieValue('react-hookz')); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined ', () => { | ||
const { result } = renderHook(() => useCookie('react-hookz')); | ||
const { result } = renderHook(() => useCookieValue('react-hookz')); | ||
expect(result.current[0]).toBeUndefined(); | ||
}); | ||
}); |
Oops, something went wrong.