相关疑难解决方法(0)

如何确定变量是"未定义"还是"空"?

如何确定变量undefined还是null?我的代码如下:

var EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
  //DO SOMETHING
};
Run Code Online (Sandbox Code Playgroud)
<div id="esd-names">
  <div id="name"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

但是如果我这样做,JavaScript解释器会暂停执行.

javascript variables null jquery undefined

2000
推荐指数
17
解决办法
240万
查看次数

在React中检查未定义

我有一个场景,我将数据从reducer传递到我的反应状态.

数据:

{
    "id": 1,
    "title": "Test",
    "content": {
        "body": "sdfsdf"
        "image": "http://example.com"
    }
}
Run Code Online (Sandbox Code Playgroud)

使用componentWillRecieveProps,这非常适合检索标题.

componentWillReceiveProps(nextProps) {
    this.setState({
        title: nextProps.blog.title,
    })
}
Run Code Online (Sandbox Code Playgroud)

但是,我在检索嵌套字段时遇到了困难.当我这样做:

componentWillReceiveProps(nextProps) {
    console.log("new title is", nextProps.blog.title);
    console.log("new body content is", nextProps.blog.content["body"]);
    this.setState({
        title: nextProps.blog.title,
        body: nextProps.blog.content["body"]
    })
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

在此输入图像描述

单击调试器并加载内容后,未定义主体的错误消失.无论如何我能解决这个问题吗?

我试着像这样检查undefined:

if (typeof nextProps.blog.content["body"] != 'undefined'){
Run Code Online (Sandbox Code Playgroud)

但这也不起作用,我相信这是因为博客未定义.

components undefined reactjs redux

31
推荐指数
4
解决办法
7万
查看次数

标签 统计

undefined ×2

components ×1

javascript ×1

jquery ×1

null ×1

reactjs ×1

redux ×1

variables ×1