简体   繁体   English

对React子组件中父HTML元素的引用

[英]Reference to parent html element in React child component

A simple Editor.jsx component render method that renders a SVG element and a child component called <SystemBox /> 一个简单的Editor.jsx组件render方法,该方法呈现SVG元素和名为<SystemBox />的子组件

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

In SystemBox.jsx I need access to the actuall SVG html element to for example get to the getScreenCTM() method on the SVG element. SystemBox.jsx我需要访问实际的SVG html元素,例如,获取SVG元素上的getScreenCTM()方法。

How do I pass a reference to the SVG element in the parent to the child component? 如何将对父组件中SVG元素的引用传递给子组件?

I guess I could use document.getElementById('editor') in the SystemBox.jsx child component, but that doesn't right. 我想我可以在SystemBox.jsx子组件中使用document.getElementById('editor') ,但这是不对的。

Take a look at refs . 看一下裁判 You can create a reference to your svg DOM element and pass this reference by props to any custom component you've created. 您可以创建对svg DOM元素的引用,并通过props将该引用传递给您创建的任何自定义组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM