© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•4y ago•
1 reply
Mathias

How to listen for changes and update state?

I'm would like to listen for changes in the database, and when a specific column has changes with a specific id, then I would like to update the state "role".

Here is my current code:

 const { isLoading, user, error } = useUser();
 const [role, setRole] = useState(null);

  async function fetchRole() {
    const data = await supabaseClient
      .from("roles")
      .select("role")
      .eq("id", user?.id)
      .single();
    setRole(data?.data?.role);
  }

  useEffect(() => {
    fetchRole();
  }, [role]);
 const { isLoading, user, error } = useUser();
 const [role, setRole] = useState(null);

  async function fetchRole() {
    const data = await supabaseClient
      .from("roles")
      .select("role")
      .eq("id", user?.id)
      .single();
    setRole(data?.data?.role);
  }

  useEffect(() => {
    fetchRole();
  }, [role]);

How can I listen for changes in the database and then change the state? I have looked at .subscribe() but I'm not sure how to implement it.
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Unable to listen to postgres_changes
SupabaseSSupabase / help-and-questions
2mo ago
Listen changes to a specific column
SupabaseSSupabase / help-and-questions
4y ago
Listen to single row changes with v2-client
SupabaseSSupabase / help-and-questions
4y ago
Cancelling statement due to statemtn timeout
SupabaseSSupabase / help-and-questions
7mo ago