React antd 学习随笔

Table

控制是否展开,expandedRowKeys数组存放对应展开行的数据key

expandedRowKeys = {this.state.expandedRowKeys}

控制是否单独单元格放置展开图标

expandIconAsCell = {false}

number设置展开图标所在的单元格索引,取一个不存在的索引可达到去掉预设图标的效果

expandIconColumnIndex = {-1}

展开行内为Form表单,双向绑定导致input焦点丢失

具体原因尚未清楚,表现疑似展开行内input双向绑定触发了父组件Table的重新渲染,进而触发展开行内重新渲染,导致焦点丢失,无法持续输入。
应急方案:父组件使用生命周期控制渲染

shouldComponentUpdate(nextProps, nextState) {
        if (JSON.stringify(nextState) == JSON.stringify(this.state)){
            return false;
        }
        return true;
}

缺点:部分props和state的复杂数据类型的变化无法被shouldComponentUpdate捕获,无法正确触发视图变化,因而必须保证清晰掌握父组件以及子组件所有更新可能。比如父组件中存在下面的上传组件,三元运算符结果显示失效

<Upload ……>
{
        this.state.fileList[record.key] !== undefined ?
                <Button>
                        <Icon type="upload"/>{this.state.fileList[record.key].name
                </Button>
                 :
        <Button>
            <Icon type="upload"/>上传
        </Button>
}
</Upload>

在该上传组件中,通过beforeUpload函数更新state的fileList(Object)并没有触发视图变化。通过在shouldComponentUpdate中输出控制台发现,nextState和this.state的fileList一致为更新后的值,原因未知。为了解决这个问题,我在state中引入了一个Boolean值,用于通过beforeUpload改变它来触发shouldComponentUpdate。

inputNumber

输入非数字时,在失去焦点后onChange获得的value会变成undefined,而不是空字符串或者0

Modal

个人常用例子

<Modal
        visible={this.state.visible}
        maskClosable = {false}
        onCancel={this.modalClose}
        title="提交确认"
        footer={[
                <Button key="submit" type="primary" size="large" onClick={this.submit} loading={this.state.confirmLoading}>
                        确定
                </Button>,
                <Button key="back" size="large" onClick={this.modalClose}>取消</Button>
        ]}
>
        <p>{this.state.modalContent}</p>
 </Modal>

多个modal 显示时可能存在覆盖顺序错误

通过设置modal的zIndex值,将需要的modal层次提高

Form

个人常用例子

checkError = (id) => {
  // Only show error after a field is touched.
  return this.props.form.isFieldTouched(id) && this.props.form.getFieldError(id)
}

...

<FormItem
  validateStatus={this.checkError('title') ? 'error' : ''}
  help={this.checkError('title') || ''}
  label={(
    <span>
      论文题目
    </span>
  )}
>
  {getFieldDecorator('title', {
     rules: [{ required: true, message: '标题不能为空!', whitespace: true }],
  })(
    <Input placeholder="Title" />
  )}
</FormItem>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容