React Query is still worth using with Next 14. Server actions and components are great, but with React Query, you can revalidate data live on your page. It’s like having 50 Shades of cache and the ability to control and refetch data in a flash. It’s the best of both worlds, baby! So, keep using React Query and enjoy the ride. π
Table of Contents
ToggleIntroduction π
Hey there gorgeous friends on the internet, how’s life? Hopefully, it’s all good with you. It’s Sow One here. I can’t get out of the house; Scotland is stuck in the North Pole, and Santa’s nowhere to be found! In today’s episode, I want to address a question that you guys asked me so many times: is React Query still worth using with Next.js, especially now that we have server actions?
Setting Up Next 14 Project π
I have an x14 project set up and a couple of other things like the database to start fetching some stuff. I’ll guide you through everything, and the full code will be on GitHub. But just follow along and let’s have fun!
Creating Components in Next 14 π±
Let’s create an input component to create a post. We’ll have a form with input and a submit button. I’m using React Hook Form, and I’ll walk you through the code.
Post Form Code π»
I’m using useClient
and shat CN
and import a bunch of forms and form validation. We’ll create a form and tell it to accept one value, then execute and reset the form on submit.
Fetching Posts with Next Safe Actions π¦
I’ve set up a server folder called actions. I’m fetching the post using a simple function with drizzle to get all the posts with the authors and likes, and then returning it.
Client-Side Fetching with React Query β‘
We can create a Singleton for query client and wrap it in a query client provider. Then, we can use useGetPost
to extract the data and handle errors.
Implementing Additional Features π οΈ
I added likes and remove buttons. Now, if a post gets removed from the backend, it will disappear automatically after a few seconds, even if it’s made by someone else. This episode’s code will be posted on GitHub, and I hope you enjoy it.
Conclusion βοΈ
I hope this episode provided insights into why I still use React Query with Next 14. Drop a like, hop on Discord, let’s have a chat! See you in the next one. Bye-bye! Peace ππ
Related posts:
- “How to utilize CancellationToken in ASP.NET C# API for better control over asynchronous operations?”
- “Discover the history of Kotlin with Andrey Breslav in the Brighton Kotlin event.”
- Is PHP a Legitimate Language? | Coffeezilla’s Take on PBD [My Response]
- Build multi-tenant NextJS apps easily with Clerk, designed for human-friendly, SEO-friendly development.
- Journey to the West Fiction: An Action-Packed Tale
- The reason why you should learn NextJS 14 in 2024.