无法读取 null React 的属性‘style’
2019-01-14
19626
我是新手做出反应的,当我添加一个带有以下代码的侧栏的组件时,我收到了错误:
145741344
359673829
是以下部分中的错误?
433742577
有什么问题?
如果有人可以帮助它,它将非常明显。
3个回答
首先,在 DOM 完全加载之前无法访问文档元素,因此您需要添加检查来判断元素是否已加载:
class Sidebar extends React.Component {
state = {};
//Here you are not binding your methods nor you are using the ES6 Arrow function so you need to change the code here also.
openNav = () => {
if (
document.getElementById("mySidenav") &&
document.getElementById("main")
) {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}
};
closeNav = () => {
if (
document.getElementById("mySidenav") &&
document.getElementById("main")
) {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
}
};
render() {
return (
<div>
<div id="mySidenav" class="sidenav">
//Here you are calling the function on initial render that is why you are getting the error. you need to pass the function like this in order to trigger it on an event.
<a href="javascript:void(0)" class="closebtn" onclick={this.closeNav}>
×
</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div id="main">
<h2>Sidenav Push Example</h2>
<p>
Click on the element below to open the side navigation menu, and
push this content to the right.
</p>
<span
styles={{ fontSize: "30", cursor: "pointer" }}
//Same as above here also
onclick={this.openNav}
>
☰ open
</span>
</div>
</div>
);
}
}
更正后的组件将是这个。 您应该了解一些有关 ES6 和类的概念。
Harish Soni
2019-01-14
您正尝试使用 vanilla JS 环境中的反模式来获取 DOM 节点上的链接。请参阅 React docs 以了解 refs 的使用方法。
由于 React 有自己的 DOM 导航环境,因此您应该只使用它。更新您的代码,如下所示:
import React, { Component } from "react";
import "../css/sidebar.css";
class Sidebar extends Component {
constructor(props){
super(props)
this.state = {}
this.mySidenav = React.createRef()
this.main = React.createRef()
}
openNav() {
this.mySidenav.current.style.width = "250px";
this.main.current.style.marginLeft = "250px";
}
closeNav() {
this.mySidenav.current.style.width = "0";
this.main.current.style.marginLeft = "0";
}
render() {
return (
<div>
<div id="mySidenav" ref={this.mySidenav} class="sidenav">
<a
href="javascript:void(0)"
class="closebtn"
onclick={this.closeNav}
>
×
</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div id="main" ref={this.main}>
<h2>Sidenav Push Example</h2>
<p>
Click on the element below to open the side navigation menu, and push this content to the right.
</p>
<span
styles={{ fontSize: "30", cursor: "pointer" }}
onclick={this.openNav}
>
☰ open
</span>
</div>
</div>
);
}
}
Sviat Kuzhelev
2019-01-14
在 DOM 加载之前无法访问文档元素,因此您需要检查元素是否已加载
if (
document.getElementById("mySidenav") &&
document.getElementById("main")
) {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}
或使用钩子 - useEffect()
useEffect(() => {
document.getElementById("togBtn").style.cursor = "pointer";
}, []);
SouMitya chauhan
2020-10-29