You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用react-img-mapper后出现Invalid hook call error的问题排查

解决react-img-mapper的Invalid hook call错误及空白页问题

以下是针对你遇到问题的排查和解决步骤:

1. 检查React版本兼容性

react-img-mapper内部使用了React Hooks,要求项目必须使用React 16.8.0及以上版本。如果你的React版本低于该范围,就会触发Invalid hook call错误。

解决:

升级React和React-DOM到符合要求的版本:

npm install react@^16.8.0 react-dom@^16.8.0 --save

或直接升级到最新稳定版:

npm install react@latest react-dom@latest --save

2. 修复依赖安装异常

依赖包缓存损坏或安装不完整也可能导致Hook调用错误。

解决:

删除本地依赖文件后重新安装:

# 删除node_modules和锁文件
rm -rf node_modules package-lock.json
# 重新安装所有依赖
npm install

3. 验证coords.json格式正确性

react-img-mapper对传入的map参数有严格格式要求,必须是包含name和areas数组的对象,每个area元素需要指定shape、coords和name字段。如果JSON格式不符合规范,会导致组件内部逻辑出错,进而引发Hook错误和空白页。

正确的JSON格式示例:

{
  "name": "world-map",
  "areas": [
    {
      "shape": "poly",
      "coords": [120, 340, 150, 360, 180, 330],
      "name": "country-1"
    },
    {
      "shape": "rect",
      "coords": [200, 100, 250, 150],
      "name": "country-2"
    }
  ]
}

4. 确认组件导入方式

部分版本的react-img-mapper可能需要使用命名导入而非默认导入,尝试修改导入语句:

// 替换原来的默认导入
import { ImageMapper } from 'react-img-mapper';

内容的提问来源于stack exchange,提问作者Amirosein Khd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 02:50:30