有条件地在 React 中进行输入

Conditionally make an input required in React

在My React app中的一个表单中,我想做一组输入(Length & Gauge)required,如果Category设置为roll.How可以我这样做?提前致谢。

 <Form.Group as={Col}>
   <label>Category</label>
   <Form.Control
     as="select"
     name="category"
     defaultValue={this.state.category}
     onChange={this.catControl}
   >
    <option>printed</option>
    <option>roll</option>
  </Form.Control>
</Form.Group>

<Form.Row>
 <Form.Group as={Col}>
   <label>Length(cm)</label>
   //required if category is set to "roll". How can I do that?
   <Form.Control name="length" defaultValue={this.state.length} />
 </Form.Group>

 <Form.Group as={Col}>
   <label>Gauge(mm)</label>
   <Form.Control name="gauge" defaultValue={this.state.gauge} />
 </Form.Group>
</Form.Row>

在 html 部分,您可以执行以下操作:

 <Form.Control name="length" defaultValue={this.state.length} required={ this.state.category==='roll'}/>