History.push a link to a new tab with react router

Yas*_*idi 16 reactjs react-router react-router-dom

执行一些逻辑后,我需要打开一个指向新选项卡的链接。
我有一个这样的按钮:

<Button
  onClick={handleSubmit}
>
  Preview
</Button>
Run Code Online (Sandbox Code Playgroud)

与 handleSubmit() 是:

<Button
  onClick={handleSubmit}
>
  Preview
</Button>
Run Code Online (Sandbox Code Playgroud)

如您所见,对于我的用例,使用该Link组件是没有意义的。

那么,有没有办法将该链接推送到新选项卡,只使用 history.push()

小智 35

React-router 的历史仅用于导航您的应用程序和一些持久性。我不明白为什么你需要用它来打开一个新标签。我认为您应该为此使用 window 对象。

const handleSubmit = () => {
  console.log("doing something");
  const win = window.open("/some-link", "_blank");
  win.focus();
}
Run Code Online (Sandbox Code Playgroud)

  • History.push 可以传递这个:----- pathname: '/link', search: value, state: {Detail: statevalue }----- 但 window.open 不能。我认为这就是为什么这个问题要使用history.push (5认同)