Next.js 在 href 链接路由之前执行 onClick 事件

huangapple go评论79阅读模式
英文:

Next.js execute onClick event before href link routing

问题

以下是您要翻译的内容:

Context

我正在使用Next.js 13,并且正在使用Apollo客户端来存储一些客户端变量。

What i'm trying to do

我尝试在导航到href位置之前执行onClick函数。

My Code

<Link
  href={`/session/${session.id}`}
  onClick={() => {            
    updateDialogVars({
      dialogOpen: ATTENDANCE_DIALOG,
    })
  }}
>
  <div>
    stuff
  </div>
</Link>

updateDialogVars是一个mutation,用于更新Apollo客户端中的响应式变量。

Question

是否可以使用Next.js Link组件在href路由之前执行onClick事件?是更改Link为div,然后在onClick函数执行后使用next router来推送路由更改的唯一选项吗?

英文:

Context

I'm using Next.js 13. And I'm using apollo client to store some client side variables.

What i'm trying to do

I'm trying to execute the onClick function prior to navigating to the href location.

My Code

&lt;Link
      href={`/session/${session.id}`}
      onClick={() =&gt; {            
        updateDialogVars({
          dialogOpen: ATTENDANCE_DIALOG,
        })
      }}
    &gt;
    &lt;div&gt;
      stuff
    &lt;/div&gt;
&lt;/Link&gt;

updateDialogVars is a mutation and updates a reactive vars in apollo client.

Question

Is it possible to use Next.js Link component and execute the onClick event before routing takes place with the href? Is the only option to change Link to a div and use next router to push the route change after the onClick function executes?

答案1

得分: 1

以下是代码的翻译部分:

This is typically achieved with a button and [router hook][1], not an anchor tag (link).

import { useRouter } from "next/router";

const SomeComponent = () => {
  const router = useRouter();

  const onClick = async () => {
    updateDialogVars(...yourConfig);
    if(someCondition) {
      await router.push(`/session/${session.id}`);
    }
  }

  return (
    <button onClick={onClick}>Label</button>
  )
}
You can also achieve a similar result with next/link.

import { useRouter } from "next/router";
import Link from "next/link";

const SomeComponent = () => {
  const router = useRouter();

  const onClick = async (event) => {
    event.preventDefault();
    updateDialogVars(...yourConfig);
    if(someCondition) {
      await router.push(event.target.href);
    }
  }

  return (
    <Link href={`/session/${session.id}`} onClick={onClick}>Label</Link>
  )
}
英文:

This is typically achieved with a button and router hook, not an anchor tag (link).

import { useRouter } from &quot;next/router&quot;;

const SomeComponent = () =&gt; {
 const router = useRouter();

 const onClick = async () =&gt; {
  updateDialogVars(...yourConfig);
  if(someCondition) {
   await router.push(`/session/${session.id}`);
  }
 }

 return (
  &lt;button onClick={onClick}&gt;Label&lt;/button&gt;
 )
}

You can also achieve a similar result with next/link.

import { useRouter } from &quot;next/router&quot;;
import Link from &quot;next/link&quot;;

const SomeComponent = () =&gt; {
 const router = useRouter();

 const onClick = async (event) =&gt; {
  event.preventDefault();
  updateDialogVars(...yourConfig);
  if(someCondition) {
   await router.push(event.target.href);
  }
 }

 return (
  &lt;Link href={`/session/${session.id}`} onClick={onClick}&gt;Label&lt;/Link&gt;
 )
}

huangapple
  • 本文由 发表于 2023年3月7日 10:12:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定