繁体   English   中英

在功能组件中初始化 Date() 时,React 抛出“无效的钩子错误”

[英]React throws "invalid hook error" when initializing Date() in a functional component

当尝试在使用“rfc”片段制作的功能组件中初始化日期 object 时,React 抛出以下错误。 我在组件中没有钩子。 let dateObj = new Date()是除了片段之外唯一添加的东西。

我已经检查了我的 React 版本并尝试重新启动服务器。 我使用 create-react-app 来创建我的 React 项目。

Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component.

 import React from 'react' function Date() { let date = new Date() return ( <div>Date</div> ) } export default Date

尝试更改您的 function 的名称。

 import React from 'react' function CurrentDate() { let date = new Date() return ( <div>Date</div> ) } export default CurrentDate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM