I have been a React dev for some time now. At work, we have now introduced Typescript which I am relatively new to.
I have created a hook for using cookies but the function I return is causing an error.
The hook (with some company stuff removed):
function useCookies(key: string) {
const [storedCookie, setStoredCookie] = useState(() => getCookieValue(key));
function setCookie(value: any, daysUntilExpiry?: number): any {
if (value) {
const valueToStore =
value instanceof Function ? value(storedCookie) : value;
setStoredCookie(valueToStore);
let cookieString = `${key}=${valueToStore}`;
if (daysUntilExpiry) {
const date = new Date();
date.setDate(date.getDate() + daysUntilExpiry);
cookieString = `${cookieString}; expires=${date}`;
}
document.cookie = cookieString;
}
}
return [storedCookie, setCookie];
}
const [loggedIn, setAuthState] = useCookies('logged_in');
setAuthState(true, 7);
The setAuthState function call gives me the error Expected an assignment or function call and instead saw an expression. Can anyone point me as to why?!