gpt4 book ai didi

javascript - React 子组件中父 html 元素的引用

转载 作者:行者123 更新时间:2023-11-28 14:29:36 25 4
gpt4 key购买 nike

一个简单的Editor.jsx渲染 SVG 元素和名为 <SystemBox /> 的子组件的组件渲染方法

render() {
return (
<svg id="editor">
<SystemBox />
</svg>
);
}

SystemBox.jsx我需要访问实际的 SVG html 元素,例如访问 getScreenCTM() SVG 元素上的方法。

如何将对父组件中的 SVG 元素的引用传递给子组件?

我想我可以使用document.getElementById('editor')SystemBox.jsx子组件,但这不对。

最佳答案

看看refs 。您可以创建对 svg DOM 元素的引用,并通过 props 将此引用传递给您创建的任何自定义组件。

关于javascript - React 子组件中父 html 元素的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52001057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com