image.png

  1. import React, {memo} from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Table } from 'antd';
  4. const children = [
  5. {
  6. title: '总分',
  7. dataIndex: 'total',
  8. key: 'total',
  9. width: 100,
  10. },
  11. {
  12. title: '语文',
  13. dataIndex: 'chinese',
  14. key: 'chinese',
  15. width: 100,
  16. },
  17. {
  18. title: '数学',
  19. dataIndex: 'math',
  20. key: 'math',
  21. width: 100,
  22. },
  23. {
  24. title: '英语',
  25. dataIndex: 'en',
  26. key: 'en',
  27. width: 120,
  28. },
  29. {
  30. title: '化学',
  31. dataIndex: 'chemistry',
  32. key: 'chemistry',
  33. width: 90,
  34. },
  35. ];
  36. const columns = [
  37. {
  38. title: '姓名',
  39. dataIndex: 'name',
  40. key: 'name',
  41. width: 100,
  42. },
  43. {
  44. title: '1月',
  45. children,
  46. },
  47. {
  48. title: '2月',
  49. children,
  50. },
  51. {
  52. title: '3月',
  53. children,
  54. },
  55. {
  56. title: '4月',
  57. children,
  58. },
  59. {
  60. title: '5月',
  61. children,
  62. },
  63. ];
  64. columns.forEach((item, index) => {
  65. if(item.children) {
  66. item.children = item.children.map(child => {
  67. return {...child, key: `${child.key}-${index}-${item.title}` }
  68. })
  69. }
  70. });
  71. TableSercieIndex.propTypes = {
  72. value: PropTypes.object.isRequired,
  73. onChange: PropTypes.func.isRequired,
  74. };
  75. function TableSercieIndex({value, onChange}) {
  76. const { loading, dataSource } = value;
  77. const attrs = {
  78. rowKey: 'id',
  79. size: 'middle',
  80. scroll: {x: 2000},
  81. loading,
  82. columns,
  83. dataSource,
  84. onChange,
  85. bordered: true,
  86. }
  87. return (
  88. <Table {...attrs}/>
  89. );
  90. }
  91. function propsEqual(prevProps, props) {
  92. const { dataSource } = prevProps.value
  93. return dataSource === props.value.dataSource
  94. }
  95. export default memo(TableSercieIndex, propsEqual);

自定义render

  1. import { Icon } from 'antd';
  2. function Status({ value = '' }) {
  3. const include = String(value).includes('-');
  4. const upDown = include ? 'arrow-down' : 'arrow-up';
  5. const style = { color: include ? '#ff4d4f' : '#52c41a' };
  6. return (
  7. <>{value} <Icon type={upDown} style={style} /></>
  8. );
  9. }
  10. const columns = [
  11. {
  12. title: '姓名',
  13. dataIndex: 'name',
  14. key: 'name',
  15. },
  16. {
  17. title: 'VIP',
  18. dataIndex: 'vip',
  19. key: 'vip',
  20. render: text => <Status value={text} />,
  21. },
  22. ];

优化点

  • memo
  • 父级的 onChange 用 useCallback包裹一下

key报错

columns & children,数据项中的 key不能一样,否则报错

  • 提交一个空数据时,react循环,也可能会出现错误警告
  • 或者,只要去掉重复的选项的key,就不会报错
    1. {
    2. title: '姓名',
    3. dataIndex: 'name',
    4. key: 'name', // key必须是唯一的,不能有一样的
    5. width: 90,
    6. },
    Encountered two children with the same key, name. 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

require-default-props

ESLint: propType”onChange”isnotrequired, buthasnocorrespondingdefaultPropsdeclaration.(react/require-default-props)

  1. TableList.propTypes = {
  2. value: PropTypes.object,
  3. onChange: PropTypes.func.isRequired,
  4. };
  5. // 增加 .defalutProps解决 eslint报错
  6. TableList.defaultProps = {
  7. value: {},
  8. }
  9. function TableList({value, onChange}) {}