React onClick 路由仅有时有效
这是视频证明。 https://dsc.cloud/leonardchoo/ Screen-Recording-2022-03-08-at-17.25.58.mov
我在单击按钮时遇到了一个神秘的错误对于导航,会触发“onClick”事件,但它不会重定向和呈现目标组件。
正如您在屏幕截图中看到的,onClick
事件已记录,但重定向并未发生。
中重现了这里的情况。
我在 CodeSandbox堆栈
- React TS
- Mantine UI
- React Router V5
如何解决这个问题?
Here is the video proof.
https://dsc.cloud/leonardchoo/Screen-Recording-2022-03-08-at-17.25.58.mov
I'm running into a mysterious error where I click the button for navigation, "onClick" event is fired but it does not redirect and render the target component.
As you can see in the screenshot, the onClick
event is logged, but the redirect does not happen.
I reproduced the situation here in CodeSandbox.
Stack
- React TS
- Mantine UI
- React Router V5
How can I solve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在代码中注意到的第一件事是,使用导航逻辑在每个路由组件周围渲染一个
WrapperPage
组件。我尝试尽可能简化WrapperPage
代码。采取的步骤:
header
和navbar
道具重构为独立组件,以防生成 JSX 时出现问题App< 中包装
Switch
组件/code> 使用单个WrapperPage
而不是每个路由组件问题仍然存在。
接下来,我从
@mantine/core
中删除了UnstyledButton
,因此仅渲染了Link
组件,并且无法重现。然后我尝试了普通 HTML 按钮而不是UnstyledButton
,他们再次重现了该问题。因此,在另一个交互式元素(即
按钮
)中渲染交互式元素(即来自Link
的锚标记)似乎是一个问题来自UnstyledButton
)这是一个问题。交换元素顺序,即Link
包装UnstyledButton
,似乎可以减少问题。我似乎无法用这种方式构建 DOM 来重现问题。标题
First thing I noticed in your code was that is is rendering a
WrapperPage
component around each routed component with the navigation logic. I tried simplifying theWrapperPage
code as much as possible.Steps Taken:
header
andnavbar
props into standalone components in case there was issue generating JSXSwitch
component inApp
with a singleWrapperPage
instead of each routed componentThe issue persisted.
I next removed the
UnstyledButton
from@mantine/core
so only theLink
components were rendered, and could not reproduce. I then tried vanilla HTML buttons instead of theUnstyledButton
and they again reproduced the issue.So it seems it is an issue with rendering an interactive element (i.e. anchor tag from
Link
) within another interactive element (i.e.button
fromUnstyledButton
) that is an issue. Swapping the element order, i.e.Link
wrapping theUnstyledButton
, appears to reduce the issue. I can't seem to reproduce the issue with the DOM structured this way.Header
Navbar
WrapperPage