如何动态设置antd switch 组件的valuePropName和initialValue

step 1、在父组件中:

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

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

step 2:在调用子组件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="禁用"/>
 )}

运行报错,valuePropName:{checkState}这里应该怎样写才能动态设置呢?万分感谢!

阅读 17.2k
1 个回答

官方文档)描写的很清楚呀:

const { form,checkState,enable} = this.props;
const { getFieldDecorator } = this.props.form;
{getFieldDecorator('status', {
      rules: [{ required: false}],
      valuePropName: 'checked',
      initialValue: enable
                                        
 })(
    <Switch checkedChildren="启用" unCheckedChildren="禁用"/>
 )}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题