如何在 Next.js 应用程序路由上通过按钮点击设置Cookies。

huangapple go评论137阅读模式
英文:

How to set cookies on button click on next.js app route

问题

I tried to set cookies with the code below.

import Image from "next/image";
import styles from "./page.module.css";
import { cookies } from "next/headers";

export default function Home() {
  function setCookie() {

    //code to set cookies

  }

  return (
    <div>
      <div>hello</div>
      <button onClick={setCookie}>Hello</button>
    </div>
  );
}

But it shows error Uncaught Error: Event handlers cannot be passed to Client Component props. <button onClick={function} children=...>; If you need interactivity, consider converting part of this to a Client Component.

so I added 'use client' in the top of the file.

After that it showed another error You're importing a component that needs next/headers. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component

I am following the documentation of next.js app route (not page route). And I am not getting where I went wrong. Please help. I am new to next.js

英文:

I tried to set cookies with the code below.

import Image from &quot;next/image&quot;;
import styles from &quot;./page.module.css&quot;;
import { cookies } from &quot;next/headers&quot;;

export default function Home() {
  function setCookie() {

    //code to set cookies

  }

  return (
   &lt;div&gt;
      &lt;div&gt;hello&lt;/div&gt;
      &lt;button onClick={setCookie}&gt;Hello&lt;/button&gt;
    &lt;/div&gt;
  );
}

But it shows error Uncaught Error: Event handlers cannot be passed to Client Component props.
&lt;button onClick={function} children=...&gt;
If you need interactivity, consider converting part of this to a Client Component.

so I added &#39;use client&#39; in the top of the file.

After that it showed another error You&#39;re importing a component that needs next/headers. That only works in a Server Component but one of its parents is marked with &quot;use client&quot;, so it&#39;s a Client Component

I am following the documentation of next.js app route (not page route). And I am not getting where I went wrong. Please help. I am new to next.js

答案1

得分: 1

Check this: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
This allows us to use server component function in client component through action props.

Example:

next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = { experimental: { serverActions: true } };

module.exports = nextConfig

/app/page.tsx

import { Button } from "@/components/Button";

export default function Home() {
  return (
    <div>
      <div>hello</div>
      <Button />
    </div>
  );
}

/components/Button.tsx

"use client";
import { setCookie } from "./setCookie";

export function Button() {
  return (
    <form action={setCookie}>
      <button type="submit">Hello</button>
    </form>
  );
}

/components/setCookie.tsx

"use server";
import { cookies } from "next/headers";

export async function setCookie() {
  cookies().set("foo", "bar");
}
英文:

Check this: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
This allows us to use server component function in client component through action props.

Example:

next.config.js

/** @type {import(&#39;next&#39;).NextConfig} */
const nextConfig = { experimental: { serverActions: true } };

module.exports = nextConfig

/app/page.tsx

import { Button } from &quot;@/components/Button&quot;;

export default function Home() {
  return (
    &lt;div&gt;
      &lt;div&gt;hello&lt;/div&gt;
      &lt;Button /&gt;
    &lt;/div&gt;
  );
}

/components/Button.tsx

&quot;use client&quot;;
import { setCookie } from &quot;./setCookie&quot;;

export function Button() {
  return (
    &lt;form action={setCookie}&gt;
      &lt;button type=&quot;submit&quot;&gt;Hello&lt;/button&gt;
    &lt;/form&gt;
  );
}

/components/setCookie.tsx

&quot;use server&quot;;
import { cookies } from &quot;next/headers&quot;;

export async function setCookie() {
  cookies().set(&quot;foo&quot;, &quot;bar&quot;);
}

huangapple
  • 本文由 发表于 2023年6月15日 02:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定