reactjs - 如果不为空,则反应分配值
问题描述
当我执行以下操作时:
let ContentAreaHeight = document.getElementById('contentArea').clientHeight;
我收到Cannot read property 'clientHeight' of null
错误,第一次加载页面,我收到此错误是正常的。所以,我试图提出一个空值检查,如果不为空值,就像下面的选项之一一样,但两者都不起作用,仍然给出同样的错误。
选项1
let ContentAreaHeight = (document.getElementById('contentArea').clientHeight) || 0;
选项2
let ContentAreaHeight = (document.getElementById('contentArea').clientHeight !== null) ?
document.getElementById('contentArea').clientHeight : 0;
我搜索了很多地方都没有找到解决方案,这似乎很容易但没有什么真正有效..
我将此代码放在
const content = ({ pageURL, dataStatus }) => {
和
之间return (
解决方案
你应该在打电话之前检查.clientHeight
let ContentAreaHeight = document.getElementById('contentArea');
if (ContentAreaHeight) ContentAreaHeight = ContentAreaHeight.clientHeight
推荐阅读
- javascript - javascript中的实际数组和它的反转如何相同
- android - 如何在设定的时间之前启动警报?
- javascript - 我们可以在运行时传递浏览器堆栈功能吗?
- sql - 使用 Snowflake 中另一个表中的随机值更新所有列行
- c# - 用户空闲时间后的 Asp .net 核心自动注销
- php - 如何在数据库中搜索多个单词
- javascript - 如何将对象从 node.js 发送到前端 javascript
- xamarin.forms - Xamarin/Android 资源文件具有对 Xamarin.Auth 的引用
- javascript - 在 Google 地图上标记位置
- android - webview loadUrl(内容提供者示例