0

I'm typically a back-end programmer so to assist with some of the initial work i got a project that had a template project set up. I'm trying to understand some things as my first React Native Project and i can't seem to understand what this logic is saying for bgColor? :

const navbarStyles = [
      styles.navbar,
      bgColor && { backgroundColor: bgColor }
    ];

Any explanations or references that could help? Appreciate it!

StingRay21
  • 342
  • 5
  • 15
  • 1
    bgColor && { backgroundColor: bgColor } will return null if bgColor is null, and return { backgroundColor: bgColor } if bgColor is not null. it's just && operator – tuan nguyen Jun 11 '21 at 02:38

1 Answers1

1

It's called short-circuiting and is a short-hand way of doing:

if (bgColor) { //implies bgColor != undefined (and also != null, != 0 etc)
    backgroundColor: bgColor
}

Vlad L
  • 1,544
  • 3
  • 6
  • 20
  • Perfect. Thank you very much! I found this on short-circuiting: https://stackoverflow.com/questions/9344305/what-is-short-circuiting-and-how-is-it-used-when-programming-in-java – StingRay21 Jun 11 '21 at 03:24