简体   繁体   中英

Conditionally rendering a component

const TABS = [
  {
    component: Home,
    label: "Home",
    isActive: true
  },
  {
    component: About,
    label: "About",
    isActive: false
  },
  {
    component: Contact,
    label: "Contact",
    isActive: false
  }
];

...
render() {
  return (
    {TABS.map(({ component, isActive}) => isActive ? /* how do i do it? */ : "false" )}
  )
}

I'm unsure on how to render a component as whatever I do seems to result in an error. I'm new to React so any help is appreciated.

Thanks!

You can do like this:

render() {
  return (
    {TABS.map(({ component, isActive}) => isActive ? <component /> : null )}
  )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM