0

I have a react app which show items from a nested json using map function, however one of the categories can be empty which crashes the whole application, I have tested the foreach function for the same json using js's for each function and it worked perfectly, I am trying to translate the same logic which would solve the empty category issue.

here is my for each test on the same json but with lists:

Test1

let obj = {
  "name": "Menu",
  "children": [
    {
      "type": "category",
      "name": "Burgers",
      "children": [
        {
          "type": "item",
          "name": "Burger 1",
          "children": [
            {
              "type": "modifier",
              "name": "Promo",
              "children": [
                {
                  "type": "item",
                  "name": "Promo 1"
                }
              ]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [
                {
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [
                {
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Tomato"
                },
                {
                  "type": "ingredient",
                  "name": "Pickles"
                }
              ]
            }
          ]
        },
        {
          "type": "item",
          "name": "Burger 2",
          "children": [
            {
              "type": "modifier",
              "name": "Promo",
              "children": [
                {
                  "type": "item",
                  "name": "Promo 1"
                }
              ]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [
                {
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [
                {
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Tomato"
                },
                {
                  "type": "ingredient",
                  "name": "Pickles"
                }
              ]
            }
          ]
        },
        {
          "type": "item",
          "name": "Coming Soon Offers"
        }
      ]
    },
    {
      "type": "category",
      "name": "Pizzas",
      "children": [
        {
          "type": "item",
          "name": "Pizza 1",
          "children": [
            {
              "type": "modifier",
              "name": "Promo",
              "children": [
                {
                  "type": "item",
                  "name": "Promo 1"
                }
              ]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [
                {
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [
                {
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Mashrooms"
                },
                {
                  "type": "ingredient",
                  "name": "Olives"
                }
              ]
            }
          ]
        },
        {
          "type": "item",
          "name": "Pizza 2",
          "children": [
            {
              "type": "modifier",
              "name": "Promo",
              "children": [
                {
                  "type": "item",
                  "name": "Promo 1"
                }
              ]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [
                {
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [
                {
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Mashrooms"
                },
                {
                  "type": "ingredient",
                  "name": "Olives"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}
let body = document.querySelector('body');
function print(obj){
  let str = `<li>${obj.name}</li>`;
  if(obj.children){
    str += '<ul>' 
    for(let c of obj.children) str += print(c)
    str += '</ul>'
  }
  return str;
}
document.body.innerHTML = print(obj);

Test 2

const buildMenu = (data) => {
  let ul = document.createElement('ul');
  data.children.forEach(i => {
    let li = document.createElement('li');
    li.innerText = i.name;
    li.className = i.type;
    if (i.children) li.appendChild(buildMenu(i));
    ul.appendChild(li);
  });

  return ul;
};

let data = {

  "name": "Menu",
  "children": [{
      "type": "category",
      "name": "Burgers",
      "children": [{
          "type": "item",
          "name": "Burger 1",
          "children": [{
              "type": "modifier",
              "name": "Promo",
              "children": [{
                "type": "item",
                "name": "Promo 1"
              }]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [{
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [{
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Tomato"
                },
                {
                  "type": "ingredient",
                  "name": "Pickles"
                }
              ]
            }
          ]
        },
        {
          "type": "item",
          "name": "Burger 2",
          "children": [{
              "type": "modifier",
              "name": "Promo",
              "children": [{
                "type": "item",
                "name": "Promo 1"
              }]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [{
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [{
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Tomato"
                },
                {
                  "type": "ingredient",
                  "name": "Pickles"
                }
              ]
            }
          ]
        },
        {
          "type": "item",
          "name": "Coming Soon Offers"
        }
      ]
    },
    {
      "type": "category",
      "name": "Pizzas",
      "children": [{
          "type": "item",
          "name": "Pizza 1",
          "children": [{
              "type": "modifier",
              "name": "Promo",
              "children": [{
                "type": "item",
                "name": "Promo 1"
              }]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [{
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [{
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Mashrooms"
                },
                {
                  "type": "ingredient",
                  "name": "Olives"
                }
              ]
            }
          ]
        },
        {
          "type": "item",
          "name": "Pizza 2",
          "children": [{
              "type": "modifier",
              "name": "Promo",
              "children": [{
                "type": "item",
                "name": "Promo 1"
              }]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [{
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [{
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Mashrooms"
                },
                {
                  "type": "ingredient",
                  "name": "Olives"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
};

document.body.appendChild(buildMenu(data));

1 Answer 1

1

You are missing object empty check in multiple places.

  1. In your ItemList component's render function there's an else statement where you assign selectedChild and selectedItem.You need to check if activelist.children and selectedChild.children is empty or not
   const selectedChild = activelist.children.length ? activelist.children[this.state.selected] : null;
   const selectedItem = selectedChild.children && selectedChild.children.length
        ? selectedChild.children[this.state.itemSelected]: null;
  1. To render selectedChild you should check if children is empty or not.
{
  selectedChild &&
  selectedChild.children &&
  selectedChild.children.length &&
  selectedChild.children.map(
    (item, index) => (
      <Item
        className="person"
        key={index}
        Title={item.name}
        onClick={this.handleClick}
        index={index}
      />
   )
)}
  1. To render selectedItem you should check if children is empty or not
<div>
  { selectedItem &&
    selectedItem.children &&
    selectedItem.children.map((item, index) => (
      <Modifiers
        key={index}
        title={item.name}
        myKey={index}
        options={item.children}
        childk={item.id}
       />
     ))
  }
</div>
<div>
  { 
    selectedItem &&
    selectedItem.size &&
    (<div>
        <Size
           options={selectedItem.size}
           sizetitle={"Size"}
        />
       </div>)
   }
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.