Tutorials/NextJS/Getting Started
Lesson

Mutating Data

Getting Started/NextJS

๐Ÿ‘‡


๐Ÿš€ Next.js Mutating Data (Easy Notes)

๐Ÿ“Œ 1. Mutating Data kya hota hai?

๐Ÿ‘‰ Simple:

  • Data change karna (CRUD)

    • Create

    • Update

    • Delete


โšก 2. Server Function kya hota hai?

๐Ÿ‘‰ Server pe run hone wala async function

๐Ÿ‘‰ Isko bolte hain:

  • Server Function

  • Server Action (form use case me)

๐Ÿ“Œ Important:

  • Always async

  • Server pe run hota hai

  • Client se call hota hai


๐Ÿ”น Kaise banaye?

๐Ÿ‘‰ "use server" use karo

plaintext
export async function createPost(formData) {
  "use server"

  const title = formData.get("title")

  // DB save
}

๐Ÿง  3. Server Action ka Use (Form me)

plaintext
import { createPost } from "./actions"

export function Form() {
  return (
    <form action={createPost}>
      <input name="title" />
      <button type="submit">Create</button>
    </form>
  )
}

๐Ÿ‘‰ Form submit โ†’ direct server call

๐Ÿ“Œ Automatically POST request hota hai


๐Ÿ’ป 4. Client Component se call kaise kare?

plaintext
"use client"
import { createPost } from "./actions"

export default function Button() {
  return <button formAction={createPost}>Create</button>
}

๐Ÿ”ฅ 5. Event Handler se call

plaintext
"use client"
import { incrementLike } from "./actions"

export default function LikeButton() {
  return (
    <button onClick={async () => {
      await incrementLike()
    }}>
      Like
    </button>
  )
}

โณ 6. Loading State (Pending)

plaintext
"use client"
import { useActionState } from "react"

const [state, action, pending] = useActionState(createPost, false)

<button onClick={action}>
  {pending ? "Loading..." : "Submit"}
</button>

๐Ÿ‘‰ pending = loading state


๐Ÿ”„ 7. Data Refresh (Important)

๐Ÿ‘‰ Data change ke baad UI update karna

plaintext
import { refresh } from "next/cache"

export async function updatePost() {
  "use server"
  // update DB
  refresh()
}

โ™ป๏ธ 8. Cache Revalidate

plaintext
import { revalidatePath } from "next/cache"

export async function createPost() {
  "use server"

  // DB save
  revalidatePath("/posts")
}

๐Ÿ‘‰ Page fresh data show karega


๐Ÿ”€ 9. Redirect after Action

plaintext
import { redirect } from "next/navigation"

export async function createPost() {
  "use server"

  redirect("/posts")
}

๐Ÿ‘‰ Action ke baad page change


๐Ÿช 10. Cookies Handle

plaintext
import { cookies } from "next/headers"

export async function example() {
  "use server"

  const cookieStore = cookies()

  cookieStore.set("name", "Anil")
}

๐Ÿ‘‰ Get / Set / Delete cookies


๐Ÿง  11. Important Rules (Interview)

  • Server Action = "use server"

  • Always async

  • Form โ†’ direct call

  • POST request automatically

  • Secure (server side)

  • After mutation:

    • refresh / revalidate / redirect


๐Ÿ”ฅ Short Revision (1 min)

๐Ÿ‘‰ Mutate = create/update/delete

๐Ÿ‘‰ Server Action = server function

๐Ÿ‘‰ "use server" = must

๐Ÿ‘‰ form action = auto call

๐Ÿ‘‰ revalidate = refresh data


๐Ÿ’ฅ Super Simple Line

๐Ÿ‘‰ "Next.js me data change karne ke liye Server Actions use karte hain"


๐Ÿš€ Full Flow เคธเคฎเค เคฒเฅ‹

  1. Form submit

  2. Server Action call

  3. DB update

  4. UI refresh

Mutating Data | NextJS | Softcrayons Tech Solutions