DEV Community

Cover image for The magic of react-query and supabase

The magic of react-query and supabase

Ankit Jena on June 28, 2021

It's been a while since I wrote my last article on state management in React using Context. Here's the link for anyone who wants to give it a read....
Collapse
 
amodinho profile image
Amo Moloko

Excellent work. 🌟

Only thing I would do differently is to create a resuable function for storing your form state.

You use the following pattern:

//useState declearation
 const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')

  //in the JSX part of the component
          <input 
            type="text" 
            onChange={e => setEmail(e.target.value)}
        />
Enter fullscreen mode Exit fullscreen mode

Which could be upgraded to:

const [formState, setFormState] = useState({})

const updateFormState = (field,value) => {

   const newState = {
        [`${field}`]:value,
       ...formState
      }

     setFormState(newState)
}


    //JSX

      <input 
            type="text" 
            onChange={e => updateFormState("email",e.target.value)}
        />
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hmenchaca profile image
hmenchaca

Amazing!

Collapse
 
ankitjey profile image
Ankit Jena

Glad you liked it!

Collapse
 
mathewthe2 profile image
Mathew Chan

Thank you for making this tutorial. This is almost exactly what I needed, but I used NextAuth.js to create the authentication table.

Collapse
 
ankitjey profile image
Ankit Jena

So did you connect nextauth with the supabase postgres instance

Collapse
 
mathewthe2 profile image
Mathew Chan • Edited

I did. Several things I had to do different.

Instead of .env.local, I had to add the supabase environment variables to next.config.js because the client supabase is declared client side instead of server side.

With nextauth I had to do an async call getSession() to request the userId first before querying because the userId isn't exposed to the default session object for security reasons

This was also my first time using Typescript so there were all kinds of issues trying to follow the code to get it working on my own project.

Thread Thread
 
ankitjey profile image
Ankit Jena

That's awesome.

Collapse
 
spiropoulos94 profile image
NikosSp

I am a simple man, I see React-Query, I press "LOVE"

Collapse
 
ankitjey profile image
Ankit Jena

Haha

Collapse
 
aquibbaig profile image
Aquib Baig

Really useful article, Ankit! Keep up the good work

Collapse
 
ankitjey profile image
Ankit Jena

Thanks Aquib

Collapse
 
exponent42 profile image
exponent42

This is excellent thank you

Collapse
 
exponent42 profile image
exponent42

This pattern continues to replace 99% of my state boilerplate. Thanks again Ankit, really

Collapse
 
reubence profile image
Reuben Rapose

Thank you for this <3