Search code examples
reactjsattributes

ReactJs - add attribute value for input


I need add attribute VALUE for input depending on the state of the parameter isValue

{this.state.isValue ?
<input type="text" id="amount" name="amount" value={this.state.amount} />
:
<input type="text" id="amount" name="amount" />
}

If I use this code - I have emtpy value in html code (if isValue = false)

<input type="text" id="amount" name="amount" value={this.state.isValue ? this.state.amount : null} />

Is it possible to do this in a more elegant way? For example, I try like this, but it doesn't work...

<input type="text" id="amount" name="amount" {this.state.isValue ? value=this.state.amount : null} />

Solution

  • Finally I found a solution. Too bad no one could help me.

    <input type="text" id="amount" name="amount" {...(this.state.isValue ? {value: this.state.amount} : {})} />