【发布时间】:2018-10-17 17:03:17
【问题描述】:
我正在尝试根据我引入的数据中是否存在 JSON 键来隐藏或显示 HTML 元素。如果该键存在,我想显示我输入的元素 ID对象。如果键不存在,我想隐藏相同的元素 ID。我正在使用 React,但不确定它属于 componentDidMount 还是常规函数。我是新手,如果我不小心遗漏了重要的代码或写了蹩脚的东西,我深表歉意。
我的componentDidMount 代码:
componentDidMount = () => {
// Need to hide an the HTML element if the json key doesn't exist in the object
//check the JSON object for the key "workExamples"
if (typeof props.workItemData.workExamples === "undefined") {
console.log("it did not find the json key");
// Change element styling to hidden
document.getElementById("work-examples").style.visibility = "hidden";
} else {
return;
}
};
我的 HTML:
<span id="work-examples" className="work-examples">
<a href={props.workItemData.workExamples}>Work Examples</a>
</span>
我假设我不需要发布我的 JSON 对象。我有一些;只有一个有workExamples 键。我正在使用.map() 列出对象。
使用上面的代码,它不会为没有键的 JSON 对象隐藏列表中的元素。希望这一切都有意义。感谢您提供任何帮助。
【问题讨论】:
标签: javascript json reactjs show-hide