Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for Undefined In React

I have a scenario where I'm passing data from a reducer into my react state.

data:

{     "id": 1,     "title": "Test",     "content": {         "body": "sdfsdf"         "image": "http://example.com"     } } 

Using componentWillRecieveProps, this works perfectly for retrieving the title.

componentWillReceiveProps(nextProps) {     this.setState({         title: nextProps.blog.title,     }) } 

However, I'm having difficulty retrieving the nested fields. When I do this:

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"]     }) } 

I get this error:

enter image description here

The error of an undefined body goes away after I click the debugger and the content is loaded. Is there anyway I can combat this issue?

I tried to check for undefined like this:

if (typeof nextProps.blog.content["body"] != 'undefined'){ 

But this doesn't work either and I believe it's because the blog is undefined.

like image 317
lost9123193 Avatar asked Nov 22 '16 06:11

lost9123193


People also ask

How do you check for undefined conditions in React?

To check for undefined in React, use a comparison to check if the value is equal or is not equal to undefined , e.g. if (myValue === undefined) {} or if (myValue !== undefined) {} . If the condition is met, the if block will run. Copied!

How do you check for null and undefined In React?

To check if a variable is null or undefined in React, use the || (or) operator to check if either of the two conditions is met. When used with two boolean values the || operator returns true if either of the conditions evaluate to true .

How do you know if a variable is undefined?

So the correct way to test undefined variable or property is using the typeof operator, like this: if(typeof myVar === 'undefined') .


2 Answers

What you can do is check whether you props is defined initially or not by checking if nextProps.blog.content is undefined or not since your body is nested inside it like

componentWillReceiveProps(nextProps) {      if(nextProps.blog.content !== undefined && nextProps.blog.title !== undefined) {        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"]        })     } } 

You need not use type to check for undefined, just the strict operator !== which compares the value by their type as well as value

In order to check for undefined, you can also use the typeof operator like

typeof nextProps.blog.content != "undefined" 
like image 116
Shubham Khatri Avatar answered Oct 14 '22 13:10

Shubham Khatri


I was face same problem ..... And I got solution by using typeof()

if (typeof(value) !== 'undefined' && value != null) {          console.log('Not Undefined and Not Null')   } else {          console.log('Undefined or Null') } 

You must have to use typeof() to identified undefined

like image 38
MD Ashik Avatar answered Oct 14 '22 11:10

MD Ashik