【问题标题】:Invalid Hook for react-router-dom call after following tutorial遵循教程后,react-router-dom 调用的 Hook 无效
【发布时间】:2022-01-22 10:11:55
【问题描述】:
【问题讨论】:
标签:
reactjs
react-hooks
react-router-dom
【解决方案1】:
你链接的教程是2019年的,肯定不是使用当前版本的react-router-dom!
当前版本是 v6.x,有很多重大变化。
为了专门解决您询问的问题,其中一项更改是对 Route 组件的更改。他们不再使用component, render, or childrenfunctions to render routed components, but instead use the singularelementprop that takes a JSX literal, or rather, aReactElement`,值。
<Routes>
<Route path="/" element={<Home />} />
<Route path="profile" element={<Profile />} />
<Route path="/private" element={<Private />} />
<Route path="/public" element={<Public />} />
</Routes>
我建议关注 Upgrading from v5 以了解 v4/5 语法的其余更改,您将在许多较旧的 RRD 演示和教程中看到。