Skip to content

Commit

Permalink
universal-cookie - Add support for generic type with reading cookies (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
eXon committed Jul 4, 2019
1 parent 19bc277 commit a8b53a5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Upgrade dependencies to last versions
- Publish MIT license to NPM with the code (#224)
- `universal-cookie`: Add support for generic type with reading cookies (#222)

## v4.0.0

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"webpack": "^4.16.5"
},
"scripts": {
"format": "prettier 'packages/**/*.js' 'packages/**/*.ts' 'packages/**/*.tsx' --single-quote --write",
"format": "prettier packages/**/*.js packages/**/*.ts packages/**/*.tsx --single-quote --write",
"test": "npm run build && npm run size && karma start --single-run",
"watch": "karma start",
"build": "npm run build-universal && npm run build-react && npm run build-express && npm run build-koa && npm run build-demo",
Expand Down
41 changes: 19 additions & 22 deletions packages/react-cookie/src/useCookies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,29 @@ export default function useCookies(
const [allCookies, setCookies] = useState(initialCookies);
const previousCookiesRef = useRef(allCookies);

useEffect(
() => {
function onChange() {
const newCookies = cookies.getAll();
useEffect(() => {
function onChange() {
const newCookies = cookies.getAll();

if (
shouldUpdate(
dependencies || null,
newCookies,
previousCookiesRef.current
)
) {
setCookies(newCookies);
}

previousCookiesRef.current = newCookies;
if (
shouldUpdate(
dependencies || null,
newCookies,
previousCookiesRef.current
)
) {
setCookies(newCookies);
}

cookies.addChangeListener(onChange);
previousCookiesRef.current = newCookies;
}

cookies.addChangeListener(onChange);

return () => {
cookies.removeChangeListener(onChange);
};
},
[cookies]
);
return () => {
cookies.removeChangeListener(onChange);
};
}, [cookies]);

const setCookie = useMemo(() => cookies.set.bind(cookies), [cookies]);
const removeCookie = useMemo(() => cookies.remove.bind(cookies), [cookies]);
Expand Down
2 changes: 1 addition & 1 deletion packages/react-cookie/src/withCookies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function withCookies<T extends ReactCookieProps>(
const allCookies = cookies.getAll();
return (
<WrapperComponent
{...restProps as T}
{...(restProps as T)}
ref={forwardedRef}
cookies={cookies}
allCookies={allCookies}
Expand Down
4 changes: 4 additions & 0 deletions packages/universal-cookie/src/Cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ export default class Cookies {
}
}

public get(name: string, options?: CookieGetOptions): any;
public get<T>(name: string, options?: CookieGetOptions): T;
public get(name: string, options: CookieGetOptions = {}) {
this._updateBrowserValues();
return readCookie(this.cookies[name], options);
}

public getAll(options?: CookieGetOptions): any;
public getAll<T>(options?: CookieGetOptions): T;
public getAll(options: CookieGetOptions = {}) {
this._updateBrowserValues();
const result: { [name: string]: any } = {};
Expand Down

0 comments on commit a8b53a5

Please sign in to comment.