【问题标题】:Does anyone know why my react map gl is not working有谁知道为什么我的反应地图 gl 不起作用
【发布时间】:2021-11-15 10:06:05
【问题描述】:

我想知道 react typescript 是否很好,所以我尝试了一下。 我遇到了从未遇到过的问题。

这是我的代码

function Map() {
  const [viewport, setViewport] = useState({
    width: 400,
    height: 400,
    latitude: 37.7577,
    longitude: -122.4376,
    zoom: 8
  });

  return (
    <ReactMapGL
      {...viewport}
        mapboxApiAccessToken="API key here"
      onViewportChange={(viewport: any) => setViewport(viewport)}
    />
  );
}

这是我得到的错误

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
▶ 2 stack frames were collapsed.
(anonymous function)
C:/Users/User/Drive/Coding/src/components/interactive-map.js:287
  284 | 
  285 | /* eslint-disable max-statements */
  286 | const InteractiveMap = forwardRef((props, ref) => {
> 287 |   const parentContext = useContext(MapContext);
  288 |   const controller = useMemo(() => props.controller || new MapController(), []);
  289 |   const eventManager = useMemo(
  290 |     () =>

我不确定是什么原因,如果您需要更多信息,请询问。

【问题讨论】:

    标签: javascript reactjs typescript react-map-gl


    【解决方案1】:

    如果你去https://reactjs.org/link/invalid-hook-call,你可以阅读

    不要在事件处理程序中调用 [hooks]。

    这就是你正在做的......

    【讨论】:

      【解决方案2】:

      这里的问题不在于react-map-gl,而在于您使用钩子的方式。 Hooks 只能在函数组件内部直接使用,因为 react 排序和调用它们的方式(使用数组),您可以在 React 官方网站上阅读更多信息。

      【讨论】:

      • AFAIK,钩子处理程序可能作为道具传递给另一个组件。似乎是map-gl在条件下调用了这个处理程序
      • 我认为错误很简单。在文件C:/Users/User/Drive/Coding/src/components/interactive-map.js287 行中,您在forwardRef 的回调中使用了钩子,它不是一个功能组件。
      猜你喜欢
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多