redux-cookies-middleware icon indicating copy to clipboard operation
redux-cookies-middleware copied to clipboard

reduxCookiesMiddleware serializes the string input state, which adds the unnecessary quotes to cookie value

Open ghost opened this issue 3 years ago • 0 comments

Hello, I had some issues using this middleware, so I'd like to kindly ask you to check this problem.

What I wanted to do is to save the language code (like "en" or "ko") in redux state, and load it from cookie values using your middleware.

Loading the cookie value works just fine, but when I try to change the state, the unnecessary "%22" (which stands for ") characters are added to the cookie value. As I am using 'i18next-browser-languagedetector' which directly reads the cookie and loads the value, It is hard for me to force-remove the quotes.

I found that the issue comes from the store function in "reduxCookiesMiddleware"

if (deleteCheck(nextVal)) {
    options.setCookie(state.name, JSON.stringify(nextVal), 0);
} else {
    options.setCookie(state.name, JSON.stringify(nextVal));
}

When I added the type check like this:

if (deleteCheck(nextVal)) {
    if (typeof nextVal === "string") {
        options.setCookie(state.name, nextVal, 0);
    } else {
        options.setCookie(state.name, JSON.stringify(nextVal), 0);
    }
} else {
    if (typeof nextVal === "string") {
        options.setCookie(state.name, nextVal);
    } else {
        options.setCookie(state.name, JSON.stringify(nextVal));
    }
}

the issue was solved. Therefore I'd like to ask you if you can add the type checking logic like this.

If there is anything unclear, please let me know. Thank you.

ghost avatar Feb 12 '21 11:02 ghost