Skip to content

Why can't I get the latest value of useState in subscribe? #3493

Answered by ru88s
ru88s asked this question in Questions
Discussion options

You must be logged in to vote

@VictorPeralta Thanks!
When I add editMode to the array in this code and insert it, I get console.log output over and over again.
It is not possible to get the exact value.

import { useEffect, useState } from "react";
import { useLocalStorage } from "react-use";
import { supabase } from "src/lib/initSupabase";

const Demo = () => {
  const [editMode, setEditMode] = useState(false);
  const [commentTracking, setCommentTracking] = useLocalStorage("commentTracking", false);

  useEffect(() => {
    supabase
      .from("comments")
      .on("INSERT", (payload) => {
        console.log("commentTracking: " + commentTracking);
      })
      .subscribe();
  }, [editMode, commentTracking]);

  r…

Replies: 2 comments 15 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
15 replies
@ru88s
Comment options

@VictorPeralta
Comment options

@VictorPeralta
Comment options

@ru88s
Comment options

@VictorPeralta
Comment options

Answer selected by Ziinc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants