Skip to content

Commit

Permalink
feat(react-cookie): Add CookieGetOptions to useCookies (#532)
Browse files Browse the repository at this point in the history
* feat(react-cookie): Add CookieGetOptions to useCookies

* update readme for react-cookie

* Update readme to include doNotUpdate documentation
  • Loading branch information
presto2116 authored Jul 21, 2024
1 parent b1d485b commit ad238c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 13 additions & 1 deletion packages/react-cookie/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ On the server, the `cookies` props must be set using `req.universalCookies` or `

- defaultSetOptions: You can set default values for when setting cookies.

## `useCookies([dependencies])`
## `useCookies([dependencies], [options])`

Access and modify cookies using React hooks.

Expand All @@ -66,6 +66,18 @@ const [cookies, setCookie, removeCookie] = useCookies(['cookie-name']);

Let you optionally specify a list of cookie names your component depend on or that should trigger a re-render. If unspecified, it will render on every cookie change.

### `options` (optional)

- options (object):
- doNotParse (boolean): do not convert the cookie into an object no matter what
- doNotUpdate (boolean): do not update the cookies when the component mounts

```jsx
const [cookies, setCookie, removeCookie] = useCookies(['cookie-name'], {
doNotParse: true,
});
```

### `cookies`

Javascript object with all your cookies. The key is the cookie name.
Expand Down
14 changes: 7 additions & 7 deletions packages/react-cookie/src/useCookies.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useContext, useLayoutEffect, useState, useRef, useMemo } from 'react';
import { Cookie, CookieSetOptions } from 'universal-cookie';
import { Cookie, CookieSetOptions, CookieGetOptions } from 'universal-cookie';
import CookiesContext from './CookiesContext';
import { isInBrowser } from './utils';

export default function useCookies<T extends string, U = { [K in T]?: any }>(
dependencies?: T[],
options?: CookieGetOptions,
): [
U,
(name: T, value: Cookie, options?: CookieSetOptions) => void,
Expand All @@ -15,17 +16,16 @@ export default function useCookies<T extends string, U = { [K in T]?: any }>(
if (!cookies) {
throw new Error('Missing <CookiesProvider>');
}
const defaultOptions = { doNotUpdate: true };

const [allCookies, setCookies] = useState(() =>
cookies.getAll({ doNotUpdate: true }),
);
const getOptions: CookieGetOptions = { ...defaultOptions, ...options };

const [allCookies, setCookies] = useState(() => cookies.getAll(getOptions));

if (isInBrowser()) {
useLayoutEffect(() => {
function onChange() {
const newCookies = cookies.getAll({
doNotUpdate: true,
});
const newCookies = cookies.getAll(getOptions);

if (shouldUpdate(dependencies || null, newCookies, allCookies)) {
setCookies(newCookies);
Expand Down

0 comments on commit ad238c2

Please sign in to comment.