Checking for Undefined In React

前端 未结 4 646
囚心锁ツ
囚心锁ツ 2020-12-29 20:10

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

data:

{
    \"id\": 1,
    \"title\": \"Test\",
    \"content\": {
            


        
4条回答
  •  时光取名叫无心
    2020-12-29 20:29

    In case you also need to check if nextProps.blog is not undefined ; you can do that in a single if statement, like this:

    if (typeof nextProps.blog !== "undefined" && typeof nextProps.blog.content !== "undefined") {
        //
    }
    

    And, when an undefined , empty or null value is not expected; you can make it more concise:

    if (nextProps.blog && nextProps.blog.content) {
        //
    }
    

提交回复
热议问题