Module 'react-hook-form'无useForm导出成员的TypeScript问题求助
解决Module 'react-hook-form' has no exported member 'useForm'报错
以下是针对该问题的排查和解决步骤:
确认react-hook-form已正确安装
先检查项目中是否实际安装了react-hook-form包,执行命令:npm list react-hook-form # yarn用户执行 yarn list react-hook-form如果输出中没有该包,执行安装命令:
npm install react-hook-form # 或 yarn add react-hook-form移除冲突的@types/react-hook-form依赖
react-hook-form从v7版本开始已内置TypeScript类型定义,无需单独安装@types/react-hook-form。如果之前手动安装过这个包,会和内置类型冲突导致报错,执行卸载:npm uninstall @types/react-hook-form # 或 yarn remove @types/react-hook-form清理依赖缓存并重启TypeScript服务
- 删除
node_modules文件夹和锁文件:# Mac/Linux rm -rf node_modules package-lock.json yarn.lock # Windows rmdir /s node_modules && del package-lock.json yarn.lock - 重新安装所有依赖:
npm install # 或 yarn install - 重启VS Code的TypeScript服务:按下
Ctrl+Shift+P(Windows)/Cmd+Shift+P(Mac),输入TypeScript: Restart TS Server并执行。
- 删除
修正代码中的拼写错误
你的代码中useForm配置里的defaultValus是拼写错误,正确应为defaultValues,虽然这不是当前报错的原因,但会导致表单默认值无法正常生效:const form = useForm({ defaultValues: { // 修正拼写 name: "", imageUrl: "", }, });
内容的提问来源于stack exchange,提问作者Cube Dev
相关产品推荐
相关产品推荐

