简体   繁体   English

React路由器导致无效的钩子调用

[英]React router causes invalid hook call

I have a React application that uses react-router .我有一个使用react-router的 React 应用程序。 A minimalistic version of my routing is as follows:我的路由的简约版本如下:

<Router>
   <div className="App">
      <Sidebar />
      <div className="app-container">
        <Switch>
          <Route path="/" exact render={Home} />
          <Route path="/logs" exact render={Logs} />
          <Route path="/log/:logID" exact render={Log} />
        </Switch>
      </div>
   </div>
</Router>

However, whenever I access a route that contains a React hook, my application crashes with the following error:但是,每当我访问包含 React 钩子的路由时,我的应用程序都会崩溃并显示以下错误:

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://.../react-invalid-hook-call for tips about how to debug and fix this problem.

I am able to bypass this by entering a function like render={() => <Log />} into the Route component, but doing so makes me unable to access match.params in the component.我可以通过在Route组件中输入类似render={() => <Log />}的函数来绕过这个问题,但这样做会使我无法访问match.params中的match.params Is there a fix that would allow me to use render={Log} without the error being thrown?是否有修复可以让我使用render={Log}而不会抛出错误?

Edit:编辑:

Below is an example of a Home component that would crash the app:以下是会导致应用程序崩溃的Home组件示例:

import React, { useState } from 'react'

export default function Home() {

    const [foo, setFoo] = useState('bar');

    return (
        <div>
            Home
        </div>
    )
}

The error gets thrown when useState is called.调用useState时抛出错误。 The same behaviour would be observed for any other react hooks.对于任何其他反应钩子,都会观察到相同的行为。

From react-router docs:从反应路由器文档:

When you use component (instead of render or children, below) the router uses React.createElement to create a new React element from the given component.当您使用组件(而不是渲染或子组件,如下所示)时,路由器使用 React.createElement 从给定组件创建一个新的 React 元素。 That means if you provide an inline function to the component prop, you would create a new component every render.这意味着如果您为组件 prop 提供内联函数,您将在每次渲染时创建一个新组件。 This results in the existing component unmounting and the new component mounting instead of just updating the existing component.这会导致卸载现有组件并安装新组件,而不仅仅是更新现有组件。 When using an inline function for inline rendering, use the render or the children prop (below).使用内联函数进行内联渲染时,请使用 render 或 children 道具(如下)。

You really don't want to mount your routed component every time your parent is re-rendered.您真的不想每次重新渲染父组件时都安装路由组件。 I'd suggest passing the component as a child - https://reactrouter.com/web/api/Route我建议将组件作为孩子传递 - https://reactrouter.com/web/api/Route

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

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