鍍金池/ 問(wèn)答/HTML/ 如何動(dòng)態(tài)設(shè)置antd switch 組件的valuePropName和initi

如何動(dòng)態(tài)設(shè)置antd switch 組件的valuePropName和initialValue

step 1、在父組件中:

constructor(props) {
    super(props);
    this.state = {
        checkState:'checked',
        enable:true
    }
} 

this.setState({
    checkState:'unChecked',
    enable:false
})

step 2:在調(diào)用子組件form前

<FormE ref={this.saveFormRef} checkState={this.state.checkState} enable={this.state.enable}/>

step 3:form子組件

const { form,checkState,enable} = this.props;
const { getFieldDecorator } = this.props.form;

{getFieldDecorator('status', {
      rules: [{ required: false}],
      valuePropName:{checkState},
      initialValue:{enable}
                                        
 })(
    <Switch checkedChildren="啟用" unCheckedChildren="禁用"/>
 )}

運(yùn)行報(bào)錯(cuò),valuePropName:{checkState}這里應(yīng)該怎樣寫(xiě)才能動(dòng)態(tài)設(shè)置呢?萬(wàn)分感謝!

回答
編輯回答
尛曖昧

官方文檔)描寫(xiě)的很清楚呀:

const { form,checkState,enable} = this.props;
const { getFieldDecorator } = this.props.form;
{getFieldDecorator('status', {
      rules: [{ required: false}],
      valuePropName: 'checked',
      initialValue: enable
                                        
 })(
    <Switch checkedChildren="啟用" unCheckedChildren="禁用"/>
 )}
2017年2月19日 16:44