reactjs / reactjs/react.dev

[Bug]: Example in "Adding Interactivity" works with vite but not with nextjs

未关闭
#8,083 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug: unconfirmed
主要语言
JavaScript
星标
11.8k
派生
7.9k
平均合并
1 天 11 小时
30 天内合并 PR
11

描述

Summary
Description

The below code example on the page https://react.dev/learn/adding-interactivity is deployed correctly when I build the project using vite. However, when I try to deploy the same code in a nextjs project, I get error "Event handlers cannot be passed to Client Component props."

export default function App() {
  return (
    <Toolbar
      onPlayMovie={() => alert('Playing!')}
      onUploadImage={() => alert('Uploading!')}
    />
  );
}

function Toolbar({ onPlayMovie, onUploadImage }) {
  return (
    <div>
      <Button onClick={onPlayMovie}>
        Play Movie
      </Button>
      <Button onClick={onUploadImage}>
        Upload Image
      </Button>
    </div>
  );
}

function Button({ onClick, children }) {
  return (
    <button onClick={onClick}>
      {children}
    </button>
  );
}

Please note that I am new to React and am using the articles on this website to learn React. So there could be areas where I may be wrong and I apologize for any inconvenience caused due to my ignorance of certain concepts.

I used NextJS first as it was the recommended framework. Only after the code did not deploy, I tried the same code in a new project but this time I used vite.

The error I see is in the browser as below

Runtime ErrorServer

Event handlers cannot be passed to Client Component props.
  <button onClick={function onPlayMovie} children=...>
                  ^^^^^^^^^^^^^^^^^^^^^^
If you need interactivity, consider converting part of this to a Client Component.

src/app/App.js (3:5) @ App

  1 | export default fun```

Below is the trace from the command line

 ⨯ Error: Event handlers cannot be passed to Client Component props.
  <button onClick={function onPlayMovie} children=...>
                  ^^^^^^^^^^^^^^^^^^^^^^
If you need interactivity, consider converting part of this to a Client Component.
    at stringify (<anonymous>)
    at stringify (<anonymous>)
    at stringify (<anonymous>) {
  digest: '1272121264'
}
 ⨯ Error: Event handlers cannot be passed to Client Component props.
  <button onClick={function onUploadImage} children=...>
                  ^^^^^^^^^^^^^^^^^^^^^^^^
If you need interactivity, consider converting part of this to a Client Component.
    at stringify (<anonymous>)
    at stringify (<anonymous>)
    at stringify (<anonymous>) {
  digest: '628795184'
}
 GET / 500 in 25260ms
Page

https://react.dev/learn/adding-interactivity

Details

Below are the details of my environment from the package.json

"dependencies": {
    "react": "19.1.0",
    "react-dom": "19.1.0",
    "next": "15.5.5"
  }

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Adding Interactivity 页面开始,在 Next.js 15.5.5 应用中复现该示例,然后将其与 Vite 的结果进行比较。确定针对所报告的服务器/客户端组件错误,该页面需要提供哪些指导或对示例进行何种调整;当页面能够准确说明预期情况并记录一条可行路径时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, nextjs, react
领域
documentation
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。