react中使用antd的过程即踩坑(警告信息)

53 篇文章 2 订阅
28 篇文章 2 订阅

1.在使用react-antd,Table组件时报警告,虽然不影响效果但是,对于强迫症的人来说是无法接受的。

react-dom.development.js?cada:506 Warning: Encountered two children with the same key, `null`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

报错图片

警告信息是:react-dom.development.js?cada:506警告:遇到两个具有相同key“null”的子项。键应该是唯一的,以便组件在更新期间保持其标识。非唯一键可能导致子项重复和/或被忽略-该行为不受支持,并且可能在将来的版本中更改。

这个就是说在组件渲染时子项应该加上key值,并且是唯一的,而且有的key为空,必须有key值且是唯一的(最好别用index下标来当key值)

warning.js?8bf6:6 Warning: [antd: Table] Each record in dataSource of table should have a unique `key` prop, or set `rowKey` of Table to an unique primary key, see https://u.ant.design/table-row-key

warning.js?8bf6:6 Warning: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key.

这个也是key值的警告解决方案就是给Table加上rowKey={(r, i) => (r.id)}这个属性就好了,如果还有的话就是Table的cloum里没加key选项加上就好了

 <Table columns={this.state.columns}
          scroll={{ y: 620 }}
          defaultExpandAllRows={this.state.RowStatus}
          dataSource={this.state.data}
           rowKey={(r, i) => (r.id)}
          pagination={{
            onChange: this.onChange,
            defaultCurrent:1,
            total:2,
            showQuickJumper:true,
            pageSize:1,
            current:1,
            showTotal: (total, range) => `共${total}页/${range}条数据`

          }}
        />
columns: [
      {
        title: '序号',
        align: 'left',
        width: '20%',
         key: 's', //不要认为序号就不加key值了,就是随便写一个也要加上
        render: (text, record, index) => {
          return (<div>{index + 1}</div >)
        },
      },
      {
        title: 'id',
        dataIndex: 'id',
        key: 'id',
        align: 'left',
        width: '20%',
        render: (text, record) => {
          return (<div>{record.id}</div >)
        },
      }
]

react-antd Table警告https://blog.csdn.net/weixin_44058725/article/details/124480637

2.使用版本过低有些废弃的使用了也会有警告

Warning: Please use `require("history").createHashHistory` instead of `require("history/createHashHistory")`. Support for the latter will be removed in the next major release.

警告:请使用'require(“history”).createHashHistory`而不是'require(“history/createHashHistory”)`。对后者的支持将在下一个主要版本中删除。

Warning: [antd: LocaleProvider] `LocaleProvider` is deprecated. Please use `locale` with `ConfigProvider` instead: http://u.ant.design/locale

警告:[antd:LocaleProvider]“LocaleProvider”已弃用。请将“locale”与“ConfigProvider”一起使用:http://u.ant.design/locale

这些警告只需按警告的提示改就好或者更新版

3.使用时间类别组件(DatePicker等)给其赋值(setFieldsValues)不符合其类型的值,会报Uncaught TypeError:valve locale is not a function at CalenderWrapper.

解决方法

setFieldsValues({
  date:moment(date) //需要用moment转一下,直接赋值可能会报这个错误
})

4.Warning:Failed prop type: Invalid prop `selectedValue` of type `string` supplied to `DateInput` expected `Object`

DateInput提供得value属性值应该为string类型,而传入的是object类型。

//这样写会有警告
<RangePicker value={['','']} onChange={this.onChangeDate}/> 


//正确写法
<RangePicker value={''} onChange={this.onChangeDate}/> 

    Warning: Failed prop type: Invalid prop `span` of type `string` supplied to `Col`, expected `number`

 Col的span属性值应该为number类型,而传入的是string类型

//这样有警告
<Row gutter={16}>
     <Col  span={'7'}>
          <div >col-6</div>
     </Col>
</Row>




//这样就没有
<Row gutter={16}>
     <Col  span={7}>
          <div >col-6</div>
     </Col>
</Row>

   总结这一类型错误就是组件属性的数据类型不符合只要改对就好(这是一个类型的错误)

5.Warning: You cannot set a form field before rendering a field associated with the value.

  警告:在呈现与值关联的字段之前,不能设置表单字段。

这个是使用Form表单时会出现,原因时使用表单setFieldsValue时,有的字段设置了,但Form里不存在这个field。需要保证赋值的数据中的各项要在form的field中。

示例:

Form里有两个字段name和password.

一开始setFieldsValue多了一个value。这个字段Form里没有,所以会出现这个问题。

解决方案:

赋值时的字段一定要和Form里的一致。

import React, { Component } from 'react';
import { Form,Input } from 'antd';

class Index extends Component {
    formRef = React.createRef();
    constructor(props) {
        super(props);
        this.state = {

        }
    }
    componentDidMount() {
        this.formRef.current.setFieldsValue({ //这样就会出现这个警告 因为 value这个字段,Form里不存在 
            name: 'Hi, man!',
            password:"123",
            value:"8888"
          });

  // this.formRef.current.setFieldsValue({ //这样就行了,所以需要去掉没有的字段
        //     name: 'Hi, man!',
        //     password:"123"
        //   });

   }
    numhandleChange = (value, e) => {

    }
    render() {
        console.log(this)
        return (
            <div>
                <Form ref={this.formRef}>
                    <Form.Item
                        name="username"
                        label="用户名"
                        rules={[
                            {
                                required: true,
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        name="password"
                        label="密码"
                        rules={[
                            {
                                required: true,
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                </Form>
            </div>
        )
    }

}


export default Index

antd v4 Form API

6.value.locale is not a function

给DatePicker或者 RangePicker  赋值 它的值必须是moment对象。

所以需要赋值时,用moment转换一下,出现这个错误主要就是赋值时,不是moment对象或者直接就是null、undefined。

解决方案:

给时间选择器赋值时,先判断一下,并且使用moment转换一下。

 <DatePicker defaultValue={time&&moment(time)} format={dateFormat} />
//或者三目
 <DatePicker defaultValue={time?moment(time):undefined} format={dateFormat} />

antd 报错 value.locale is not a function

7.Error:Need at least a key or a value or a label(only for OptGroup) for [object Object].

报错意思:错误:至少需要[objec Object]的键、值或标签(仅适用于OptGroup).

这个错误是因为用Option没有写value值,给Option加上value之就行了。

报错截图

8. antd table 一系列 警告 参考 react-antd Table警告

9. Warning: Instance created by `useForm` is not connected to any Form element. Forget to pass `form` prop? 

 Warning: Instance created by useForm is not connected to any Form element. Forget to pass form prop?

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Ant Design Charts 是 Ant Design 团队开发的一款基于 React 封装的图表库。它使用了 Ant Design 的设计语言和风格,提供了多种常见图表类型的组件,方便开发者在 React 项目快速实现数据可视化。 在 React 项目使用 Ant Design Charts,首先需要通过 npm 或 yarn 安装该库,并引入所需的图表组件。安装完成后,我们可以在项目使用 import 语句将所需的图表组件导入: ```javascript import { LineChart, BarChart, PieChart } from 'ant-design-charts'; ``` 接下来,可以创建一个简单的 React 组件,将图表组件作为子组件进行渲染。使用 Ant Design Charts 提供的配置项,可以通过传入数据、设置样式、设置交互行为等方式自定义图表的呈现效果。 ```javascript import React from 'react'; import { LineChart } from 'ant-design-charts'; const MyChart = () => { const data = [ { year: '1991', value: 3 }, { year: '1992', value: 4 }, { year: '1993', value: 3.5 }, { year: '1994', value: 5 }, { year: '1995', value: 4.9 }, { year: '1996', value: 6 }, { year: '1997', value: 7 }, { year: '1998', value: 9 }, { year: '1999', value: 13 }, ]; const config = { data: data, xField: 'year', yField: 'value', height: 400, xAxis: { type: 'cat', }, yAxis: { type: 'linear', min: 0, }, seriesField: '', smooth: true, }; return <LineChart {...config} />; }; export default MyChart; ``` 通过以上代码,我们创建了一个简单的折线图组件,并传入一份模拟数据和一些自定义的配置项。最后,我们将该组件进行导出,就可以在其他需要使用该图表的地方进行调用和渲染。 以上就是在 React 使用 Ant Design Charts 的简单介绍和示例。通过这个图表库,我们可以方便地在 React 项目使用 Ant Design 风格的图表组件,快速实现数据的可视化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

崽崽的谷雨

漫漫前端路,摸爬滚打

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值