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(react-cookie): Add CookieGetOptions to useCookies #532

Merged
merged 3 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
eXon marked this conversation as resolved.
Show resolved Hide resolved
- 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