From 9e5e78a57a9c6f78e1c5feee20391f166632281d Mon Sep 17 00:00:00 2001 From: Preston Straight Date: Fri, 10 May 2024 12:24:57 -0400 Subject: [PATCH 1/3] feat(react-cookie): Add CookieGetOptions to useCookies --- packages/react-cookie/src/useCookies.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/react-cookie/src/useCookies.tsx b/packages/react-cookie/src/useCookies.tsx index 7d7b64f..c1fe265 100644 --- a/packages/react-cookie/src/useCookies.tsx +++ b/packages/react-cookie/src/useCookies.tsx @@ -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( dependencies?: T[], + options?: CookieGetOptions, ): [ U, (name: T, value: Cookie, options?: CookieSetOptions) => void, @@ -15,17 +16,16 @@ export default function useCookies( if (!cookies) { throw new Error('Missing '); } + 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); From 00767fa76c4c1e369ea642bba7cef9458f9772c0 Mon Sep 17 00:00:00 2001 From: Preston Straight Date: Fri, 10 May 2024 15:53:51 -0400 Subject: [PATCH 2/3] update readme for react-cookie --- packages/react-cookie/README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/react-cookie/README.md b/packages/react-cookie/README.md index 5d32182..1605bdc 100644 --- a/packages/react-cookie/README.md +++ b/packages/react-cookie/README.md @@ -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. @@ -66,6 +66,17 @@ 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 + +```jsx +const [cookies, setCookie, removeCookie] = useCookies(['cookie-name'], { + doNotParse: true, +}); +``` + ### `cookies` Javascript object with all your cookies. The key is the cookie name. From 3da5e93c44edf7681cb2f95f8d7e49fcfb182215 Mon Sep 17 00:00:00 2001 From: Preston Straight Date: Fri, 12 Jul 2024 13:29:23 -0400 Subject: [PATCH 3/3] Update readme to include doNotUpdate documentation --- packages/react-cookie/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react-cookie/README.md b/packages/react-cookie/README.md index 1605bdc..8125f9a 100644 --- a/packages/react-cookie/README.md +++ b/packages/react-cookie/README.md @@ -70,6 +70,7 @@ Let you optionally specify a list of cookie names your component depend on or th - 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'], {