Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional attribute set with React JSX [duplicate]

I'm about to make the next my page with React and I can't find how to put a configurable attribute set into component, for example:

<ProgressBar active now={current} max={total}/>

I want to add the active attribute inside the ProgressBar only if current == total.

How can I do this?

Is there an easy way to switch between two options active and stripped? Something like this:

<ProgressBar {current==total ? stripped : active} now={current} max={total}/>

except to create a properties object and spread it {...props}

like image 264
zamuka Avatar asked Oct 05 '15 14:10

zamuka


1 Answers

If the component handles false values correctly, just always add the props:

let stripped = this.state.current === this.state.total;

 <ReactBootstrap.ProgressBar
   stripped={stripped}
   active={!stripped}
   now={this.state.current}
   max={this.state.total}
 ?>

If you absolutely have to pass one or the other, you could create an object and spread it into the element:

let props = {
  [this.state.current === this.state.total ? 'stripped' : 'active']: true,
  now: this.state.current,
  max: this.state.total,
};

<ReactBootstrap.ProgressBar {...props} />
like image 69
Felix Kling Avatar answered Oct 12 '22 22:10

Felix Kling