我试图通过选择component类并将其更改为不显示来隐藏react-js项目中的组件,但出现了以下语法错误
Syntax error: Unexpected token, expected ; (12:25)
if (window.location.pathname === "/") {
> 12 | handleChange(event){
| ^
13 |
14 | var x = document.getElementByClassName("kontaktformular");
15 | x.style.display = "none";
下面是我的代码
if (window.location.pathname === "/") {
handleChange(event) {
var x = document.getElementByClassName("kontaktformular");
x.style.display = "none";
}
} else {
x.style.display = "block";
}
发布于 2018-07-06 04:11:52
你可能想要这个:
handleChange(event) {
var x = document.getElementsByClassName("kontaktformular")[0];
if (window.location.pathname === "/") {
x.style.display = "none";
} else {
x.style.display = "block";
}
}
然而,这根本不是React的方式。正确的解决方案是不基于当前的Route呈现contact表单。
至少你会想要使用这样的东西:
render() {
return (
<div>
...
{ window.location.pathname !== "/" && <KontaktFormular /> }
...
</div>
);
}
发布于 2018-07-06 04:05:30
你应该这样做
var x = document.getElementByClassName("kontaktformular");
if (window.location.pathname === "/") {
x.style.display = "none";
} else {
x.style.display = "block";
}
https://stackoverflow.com/questions/51199057
复制相似问题