基于DOM元素单击的Next/路由器不同?

发布于 2025-02-11 21:37:08 字数 1015 浏览 5 评论 0原文

我一直在为我的平台开发Darkmode/LightMode钩子和系统,使用NextJS/React等。我遇到了一个非常奇怪的问题,在某些情况下,当某些DOM元素类型被单击与其他情况时,我会碰到LightMode闪烁。我可以尝试添加有关如何实现Darkmode的更多上下文,但是我发现真正奇怪的是,将相同的OnClick/OnMousedown事件分配给相同功能会产生不同的结果。

const onClick = (e) => {
    console.log('button clicked')
    router.replace('/dashboard')
}
const onClickPreventDefault = (e) => {
    e.preventDefault()
    console.log('button clicked preventDefault')
    router.replace('/dashboard')
}

我创建了2个不同的ONCLICK函数,以测试哪些DOM元素产生哪些结果,这些元素存在于React组件中。

就像我说的那样,我可以更深入地了解Darkmode的实现方式,但实际上我想了解为什么这些结果首先会产生不同的结果,而不管结果是什么。对我来说,我得到的结果没有意义。

这是问题和项目的工作演示,您可以在登录页面上看到一些按钮会导致闪烁一些不忽略。

https://codesandbox.io/s/brokent-broken-water-water-water-9vkugk

I have been developing a darkmode/lightmode hook and system for my platform, using nextjs/react etc. I have come across a very strange issue where in certain circumstances I get a lightmode flicker when certain dom element types are clicked vs others. I can try to add more context on how darkmode was implemented however what I find really strange is the fact that assigning the same onClick/onMouseDown event to the same function produces different outcomes.

const onClick = (e) => {
    console.log('button clicked')
    router.replace('/dashboard')
}
const onClickPreventDefault = (e) => {
    e.preventDefault()
    console.log('button clicked preventDefault')
    router.replace('/dashboard')
}

I have created 2 different onClick functions to test which dom elements produce which results, these exist in a react component.

Which Dom Elements produce the bug which do not

Like I said I could go deeper into the way darkmode was implemented but I actually want to understand why these are producing different results in the first place regardless what those results are. It does not make sense to me the results I am getting.

Here is a working Demo of the issue and the project, you can see on the login page some buttons cause the flicker some dont.

https://codesandbox.io/s/broken-water-9vkugk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亽野灬性zι浪 2025-02-18 21:37:08

按钮默认为type =“ commitd”内部表单中,如果您想要一个不触发提交的普通按钮,最简单的解决方案是明确设置prop type =“ button” 上。

此“闪烁错误”只是帖子后的页面重新加载

Buttons default to type="submit" inside forms, if you want a normal button that does not trigger the submit, the simplest solution is to explicitly set the prop type="button" on it.

This "flicker bug" is just the page reloading after the post

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文