vasanthk / react-bits

✨ React patterns, techniques, tips and tricks ✨
https://vasanthk.gitbooks.io/react-bits
Creative Commons Attribution 4.0 International
16.98k stars 1.1k forks source link

What about using an object? #82

Open angelogulina opened 7 years ago

angelogulina commented 7 years ago

Given this: https://github.com/vasanthk/react-bits/blob/master/patterns/18.conditionals-in-jsx.md What about something like this:

const sampleComponent = () => {
  const basicCondition = flag && flag2 && !flag3;
  const obj = {
    'flag4': 'Blah',
    'flag5: 'Meh'
  };
  if (basicCondition) {
    return <p>{obj[flagName] || Herp}</p>
  }
  return <p>Derp</p>
};
cytrowski commented 7 years ago

@AngeloGulina nice :D

Going further:

const sampleComponent = () => {
  const basicCondition = flag && flag2 && !flag3
  const obj = {
    'flag4': 'Blah',
    'flag5: 'Meh'
  }

  return <p>{basicCondition ? obj[flagName] || Herp : Derp}</p>
}

However I prefer using cond from lodash in such a case ;)

angelogulina commented 7 years ago

@cytrowski, honestly I didn't think about cond. Good idea, thanks!