SupabaseS
Supabase4y ago
Gary

Next.js set state after insert, NULL issue

Hi. I want to update state after inserting a new row, but when receiving the data value I get NULL instead of the entire array of data from the database. I couldn't find a v2 example of this so I have been following the next todolist example from v1 in the examples archive.

        const [items, setItems] = React.useState(fetchItems, []);

    const addItem= async () => {
        let { data: item, error } = await supabase
            .from("TableName")
            .insert([{ user_id: user.id }])
            .single();
        if (error) setError(error.message);
        else setItems([...items, item]);
    };

I can fix this by adding my fetch function after setItems, but it re-renders the entire list of items instead of just adding 1. Is there a way around that? Thanks very much.
Was this page helpful?