Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have two function calls inside ternary operator in JSX?

I am trying to render two components if the condition is met inside a ternary operator. However, only the 2nd component is rendered. How can I probably put two functions calls after the conditions?

    {
      views === "monthly"
      ? this.renderDays(),
        this.renderCells()
      : null
    }

I tried the followings (none of them works)

    {
      views === "monthly"
      ? this.renderDays(),
        this.renderCells()
      : null
    }

    {
      views === "monthly"
      ? (this.renderDays(), this.renderCells())
      : null
    }

    {
      views === "monthly"
      ? (this.renderDays(); this.renderCells())
      : null
    }
like image 750
geekydude703 Avatar asked Dec 02 '22 11:12

geekydude703


1 Answers

You could return an array of components:

{
  views === "monthly"
  ? [this.renderDays(), this.renderCells()]
  : null
}

Or if the methods return arrays itself, just spread them:

 {
  views === "monthly"
  ? [...this.renderDays(), ...this.renderCells()]
  : null
}
like image 96
Jonas Wilms Avatar answered Dec 05 '22 00:12

Jonas Wilms