reactjs - 如何从打开窗口的组件中获取新窗口对象的引用?
问题描述
我想使用此库https://github.com/rmariuzzo/react-new-window来聚焦窗口,只要单击表中的行就会打开该窗口。.focus()
通过调用创建的窗口对象有一个方法window.open()
。如何获取窗口对象的引用,以便可以调用.focus()
?
解决方案
我试图做类似的事情,但希望弹出窗口在打开时打印出来。
这是解决方案(使用 React 钩子):
import React, {useEffect, useRef} from 'react';
import NewWindow from "react-new-window";
const PopupWindow = () => {
const thisWindow = useRef(null);
// This will fire when thisWindow reference changes.
useEffect(() => {
thisWindow.current.window.focus();
// thisWindow.current.window.print(); // To print when the popup window loads.
}, [thisWindow]);
return (
<>
<NewWindow
ref={thisWindow}
>
<p>
Your content here
</p>
</NewWindow>
</>
)
}
export default PopupWindow;
推荐阅读
- reactjs - React TS 函数参数类型
- ruby - 如何在 Ruby >= 3.0.0 中始终使用自定义注释字符串为 IRB 返回值添加前缀?
- tkinter - tkinter 取消 filedialog.askdirectory() 冻结程序
- javascript - 在 puppeteer 中覆盖 navigator.languages 的问题
- php - 如何用“?”替换每个字符 用php标记一个字符串
- c - 在 minGW-W64 中一起使用 readdir() 和 stat() 来获取文件/目录信息并避免最大路径问题的正确方法?
- r - 使 lme4 中混合模型的随机部分的语法更短
- r - Github 中的“Powder”未安装(RStudio)
- php - 如何让 PHP 读取“textfile.txt”的内容?
- javascript - 使用 Javascript 设置最大数量值