Skip to content

Commit

Permalink
fix(#2073): the update will not take effect before unmount (#2074)
Browse files Browse the repository at this point in the history
Co-authored-by: lxr <1076629390@qq.com>
  • Loading branch information
liuyib and crazylxr committed Feb 23, 2023
1 parent 73ac860 commit e7682c1
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions packages/hooks/src/useCookieState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ function useCookieState(cookieKey: string, options: Options = {}) {
newOptions: Cookies.CookieAttributes = {},
) => {
const { defaultValue, ...restOptions } = { ...options, ...newOptions };
setState((prevState) => {
const value = isFunction(newValue) ? newValue(prevState) : newValue;
if (value === undefined) {
Cookies.remove(cookieKey);
} else {
Cookies.set(cookieKey, value, restOptions);
}
return value;
});
const value = isFunction(newValue) ? newValue(state) : newValue;

setState(value);

if (value === undefined) {
Cookies.remove(cookieKey);
} else {
Cookies.set(cookieKey, value, restOptions);
}
},
);

Expand Down

0 comments on commit e7682c1

Please sign in to comment.