如何访问这个 the_frame
变量?
export default class oneApp extends React.Component {
constructor (props) {
super(props);
const the_frame = document.getElementsByClassName("frame")[0];
}
oneFunc() {
the_frame.style.display = "none"; // How do I access the_frame ?
}
twoFunc() {
the_frame.style.display = "block";
}
render() {
return (
<div className="frame">Hello World</div>
)
}
}
我还尝试了 this.the_frame = document.getElementsByClassName("frame")[0];
但无法访问 the_frame
。谢谢
最佳答案
如果您想要引用 DOM 元素,则应该使用 ref
属性。
关于javascript - React组件类变量访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45264747/