Next.js Discord

Discord Forum

Check if the list value is Empty via useCookies

Answered
Atlantic mackerel posted this in #help-forum
Open in Discord
Atlantic mackerelOP
Hey guys, im trying to put the value

    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 function

how can I check value (list) is empty?
Answered by Ray
the value should be a string
View full answer

28 Replies

@Ray ts if(!cookies.posts){}
Atlantic mackerelOP
Oh Thank you anddd I want to push data if the value is empty
          cookies.posts.push("test");

I tried this but the error said:

TypeError: cookies.posts.push is not a function

how 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?
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
@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?
@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]);
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 :lolsob:
@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