首页 > 解决方案 > 在ant设计表选择中通过getCheckboxProps设置defaultChecked有什么问题?

问题描述

我正在使用一张桌子作为广播组。像这样:

<Table
  dataSource={keyData}
  columns={keyColumns}
  rowSelection={keySelection}
  pagination={false}
/>
 <Form.Item >
  {getFieldDecorator('pendingKey', {
    initialValue: data?.pendingKey,
    rules: [
      {
        required: type === 'pending_key',
        message: 'Select a key'
      }
    ]
  })(<div />) // to show eorror message
}
</Form.Item> 

现在它真的defaultSelected很有帮助getCheckboxProps

getCheckboxProps: record => ({
  defaultChecked: record.key === data?.pendingKey
})

但是当我这样做时,蚂蚁设计会显示和错误消息:

Warning: [antd: Table] Do not set `checked` or `defaultChecked` in `getCheckboxProps`. Please use `selectedRowKeys` instead. 

这到底有什么问题?如何保持输入不受控制并关闭此消息?

标签: reactjsformsantd

解决方案


添加selectedRowKeys属性rowSelection以设置选定的行

<Table
  rowSelection={{
    type: 'radio',
    selectedRowKeys: ['1'], // Use this attribute
    onChange: () => {}
  }}
  //...Omitting
/>

推荐阅读