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

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

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

问题

I tried to set cookies with the code below.

  1. import Image from "next/image";
  2. import styles from "./page.module.css";
  3. import { cookies } from "next/headers";
  4. export default function Home() {
  5. function setCookie() {
  6. //code to set cookies
  7. }
  8. return (
  9. <div>
  10. <div>hello</div>
  11. <button onClick={setCookie}>Hello</button>
  12. </div>
  13. );
  14. }

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.

  1. import Image from &quot;next/image&quot;;
  2. import styles from &quot;./page.module.css&quot;;
  3. import { cookies } from &quot;next/headers&quot;;
  4. export default function Home() {
  5. function setCookie() {
  6. //code to set cookies
  7. }
  8. return (
  9. &lt;div&gt;
  10. &lt;div&gt;hello&lt;/div&gt;
  11. &lt;button onClick={setCookie}&gt;Hello&lt;/button&gt;
  12. &lt;/div&gt;
  13. );
  14. }

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

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

/app/page.tsx

  1. import { Button } from "@/components/Button";
  2. export default function Home() {
  3. return (
  4. <div>
  5. <div>hello</div>
  6. <Button />
  7. </div>
  8. );
  9. }

/components/Button.tsx

  1. "use client";
  2. import { setCookie } from "./setCookie";
  3. export function Button() {
  4. return (
  5. <form action={setCookie}>
  6. <button type="submit">Hello</button>
  7. </form>
  8. );
  9. }

/components/setCookie.tsx

  1. "use server";
  2. import { cookies } from "next/headers";
  3. export async function setCookie() {
  4. cookies().set("foo", "bar");
  5. }
英文:

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

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

/app/page.tsx

  1. import { Button } from &quot;@/components/Button&quot;;
  2. export default function Home() {
  3. return (
  4. &lt;div&gt;
  5. &lt;div&gt;hello&lt;/div&gt;
  6. &lt;Button /&gt;
  7. &lt;/div&gt;
  8. );
  9. }

/components/Button.tsx

  1. &quot;use client&quot;;
  2. import { setCookie } from &quot;./setCookie&quot;;
  3. export function Button() {
  4. return (
  5. &lt;form action={setCookie}&gt;
  6. &lt;button type=&quot;submit&quot;&gt;Hello&lt;/button&gt;
  7. &lt;/form&gt;
  8. );
  9. }

/components/setCookie.tsx

  1. &quot;use server&quot;;
  2. import { cookies } from &quot;next/headers&quot;;
  3. export async function setCookie() {
  4. cookies().set(&quot;foo&quot;, &quot;bar&quot;);
  5. }

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:

确定