简体   繁体   English

根据 select 框 reactjs 的条件渲染元素

[英]render element base on conditon of select box reactjs

I want to change element base on render condition select value, how can I do this?我想根据渲染条件 select 值更改元素,我该怎么做? When the value of select box change, I want to replace element below of my code by another, someone may help me with react rendering condition?当 select 框的值发生变化时,我想用另一个替换我的代码下面的元素,有人可以帮助我反应渲染条件吗?

<Col span={12}>
       <Form.Item
           label='Số lượng'
           {...formItemLayout}
           labelAlign='left'
       >
           <Select
               onChange={this.handleChangeSelectDUC}
                defaultValue="Đơn lẻ"     
           >
               <Option
                   value="Đơn lẻ"
               >
                   Đơn lẻ
               </Option>
               <Option
                   value="Số lượng lớn"
               >
                   Số lượng lớn
               </Option>
           </Select>
       </Form.Item>
   </Col>
   <Col span={12}>
       {
          conditon if select value = "Đơn lẻ"
               ?
               (
               <FormItem
                   label='Số ĐT'
                   {...formItemLayout}
                   labelAlign='left'
               >
                   <Input
                       name='title'
                       onChange={this.handleChangeInput}
                   />
               </FormItem>
           ):(
               <FormItem
                   label='DS khách hàng'
                   {...formItemLayout}
                   labelAlign='left'
               >
                   <Upload {...props}>
                       <Button>
                           <Icon type="upload" /> Tải lên tệp
                       </Button>
                   </Upload>,
               </FormItem>
           )
       }
   </Col>

You need to transform the select element into controlled component , where its selected value depends on a value in your state.您需要将select 元素转换为受控组件,其中其选定值取决于 state 中的值。 Then you can specify your condition easily, eg.:然后您可以轻松地指定您的条件,例如:

{this.state.value === "Đơn lẻ" ? (
// render something
) : (
// render something else
)}

Try this.尝试这个。 Get value of your select in a variable and then compare.在变量中获取 select 的值,然后进行比较。

<Col span={12}>
    {
        value == "Đơn lẻ" ?
        <FormItem1 ....> : 
        <FormItem2 ...>
    }
</Col>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM