Check if the list value is Empty via useCookies
Answered
Atlantic mackerel posted this in #help-forum
Atlantic mackerelOP
Hey guys, im trying to put the value
but the error said:
how can I check value (list) is empty?
const [cookies, setCookie, removeCookie] = useCookies(['posts']);
if(cookies.posts.isEmpty()){
setCookie('posts', true, {
path: '/'
})
}but the error said:
TypeError: cookies.posts.isEmpty is not a functionhow can I check value (list) is empty?
28 Replies
@Ray ts
if(!cookies.posts){}
Atlantic mackerelOP
Oh Thank you anddd I want to push data if the value is empty
I tried this but the error said:
how can I fix it?
cookies.posts.push("test");I tried this but the error said:
TypeError: cookies.posts.push is not a functionhow can I fix it?
Answer
@Ray the value should be a string
Atlantic mackerelOP
hmm but I want to make list value have to do this, should I use encode/decode?
@Atlantic mackerel hmm but I want to make list value have to do this, should I use encode/decode?
use
JSON.stringify() and JSON.parse()kind in mind that the limit of the size is 4096
@Ray use `JSON.stringify()` and `JSON.parse()`
Atlantic mackerelOP
ahhh thank you 🙂
@Ray use `JSON.stringify()` and `JSON.parse()`
Atlantic mackerelOP
I saved it witth Json but the value is showing undefined
@Atlantic mackerel I saved it witth Json but the value is showing undefined
could you show the code
@Ray could you show the code
Atlantic mackerelOP
I did this
const [cookies, setCookie, removeCookie] = useCookies(['posts']);
const [post, setPost] = useState<Post[]>();
if (!cookies.posts) {
let postValue: Post = {
title: "",
description: "",
lang: "C"
}
post?.push(postValue);
setPost(post)
setCookie('posts', JSON.stringify(post), {
path: '/'
})
}@Ray what are you trying to do
Atlantic mackerelOP
I tried to temporary storage the post so that value just for test
@Atlantic mackerel I tried to temporary storage the post so that value just for test
no, I meant when do you want it to save th cookies
@Ray no, I meant when do you want it to save th cookies
Atlantic mackerelOP
I want to save the cookie when there's no data
@Atlantic mackerel I want to save the cookie when there's no data
but your initial state of post is undefine
Atlantic mackerelOP
const [cookies, setCookie, removeCookie] = useCookies(['posts']);
this?
this?
@Atlantic mackerel const [cookies, setCookie, removeCookie] = useCookies(['posts']);
this?
try this
const [cookies, setCookie, removeCookie] = useCookies(["posts"]);
const [post, setPost] = useState<Post[]>([]);
useEffect(() => {
if (!cookies.posts) {
let postValue: Post = {
title: "",
description: "",
lang: "C",
};
setPost((post) => {
post.push(postValue);
setCookie("posts", JSON.stringify(post), {
path: "/",
});
return post;
});
}
}, [cookies.posts, setCookie]);@Ray try this
ts
const [cookies, setCookie, removeCookie] = useCookies(["posts"]);
const [post, setPost] = useState<Post[]>([]);
useEffect(() => {
if (!cookies.posts) {
let postValue: Post = {
title: "",
description: "",
lang: "C",
};
setPost((post) => {
post.push(postValue);
setCookie("posts", JSON.stringify(post), {
path: "/",
});
return post;
});
}
}, [cookies.posts, setCookie]);
Atlantic mackerelOP
oh wow it works perfectly thank you!!
@Ray try this
ts
const [cookies, setCookie, removeCookie] = useCookies(["posts"]);
const [post, setPost] = useState<Post[]>([]);
useEffect(() => {
if (!cookies.posts) {
let postValue: Post = {
title: "",
description: "",
lang: "C",
};
setPost((post) => {
post.push(postValue);
setCookie("posts", JSON.stringify(post), {
path: "/",
});
return post;
});
}
}, [cookies.posts, setCookie]);
Atlantic mackerelOP
btw, how can I get that cookie?
I tried this
console.log(post[0])const [cookies, setCookie, removeCookie] = useCookies(["posts"]);
const [post, setPost] = useState<Post[]>(cookies.posts ? JSON.parse(cookies.posts) : []);
useEffect(() => {
if (!cookies.posts) {
let postValue: Post = {
title: "",
description: "",
lang: "C",
};
setPost((post) => {
post.push(postValue);
setCookie("posts", JSON.stringify(post), {
path: "/",
});
return post;
});
}
}, [cookies.posts, setCookie]);@Ray ts
const [cookies, setCookie, removeCookie] = useCookies(["posts"]);
const [post, setPost] = useState<Post[]>(cookies.posts ? JSON.parse(cookies.posts) : []);
useEffect(() => {
if (!cookies.posts) {
let postValue: Post = {
title: "",
description: "",
lang: "C",
};
setPost((post) => {
post.push(postValue);
setCookie("posts", JSON.stringify(post), {
path: "/",
});
return post;
});
}
}, [cookies.posts, setCookie]);
Atlantic mackerelOP
SyntaxError: "[object Object]" is not valid JSON
22 | const [open, setOpen] = useState(false);
23 | const [cookies, setCookie, removeCookie] = useCookies(['posts']);
> 24 | const [post, setPost] = useState<Post[]>(cookies.posts ? JSON.parse(cookies.posts) : []);
| ^
25 |
26 | useEffect(() => {
27 | if (!cookies.posts) {it didnt work

@Ray what is the value of cookies.posts
Atlantic mackerelOP
posts value
const [post, setPost] = useState<Post[]>(cookies.posts || [])Atlantic mackerelOP
oh wow it works perfectly!! thank you