0

I want the ternary operator result to return nothing if variable is false, but what would I put after the semicolon? I'm sure there is a common practice of what to do here, but this has been my makeshift approach

  render() {
    const { classes, conversation } = this.props;
    return (
        {conversation
          ? (<Box className={classes.chipHolder}>
              <Chip
                label={this.state.numOfUnviewedMessages}
                size="small"
                color="primary"
              />
            </Box>) 
          : (<div/>)
        }
    );
}
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204

1 Answers1

3

You can return null:

class App() {

  render() {
    const { classes, conversation } = this.props;
    return conversation ? (
      <Box className={classes.chipHolder}>
        <Chip
          label={this.state.numOfUnviewedMessages}
          size="small"
          color="primary"
        />
      </Box>
    ) : null)
  }
  
}

Source: https://reactjs.org/docs/conditional-rendering.html#preventing-component-from-rendering

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204