1、Cannot update a component from inside the function body of a different component

https://stackoverflow.com/questions/60526786/react-warning-cannot-update-a-component-from-inside-the-function-body-of-a-diff

2、select 数据源删除后,当前选择的item不会消失,需要手动清除一下

3、https://github.com/ant-design/ant-design/issues/18283

react 组件内存泄漏

Can’t perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

无法对未挂载的组件执行响应状态更新。这是一个不允许的操作,它表明您的应用程序内存泄漏。要修复,请在componentWillUnmount取消方法中的所有订阅和异步任务。

  • 方案1:在componentWillUnmount钩子函数在组件销毁时将异步方法撤销 ```javascript componentWillUnmount(){ this.setState = ()=> false; }

componentWillUnmount = () => { this.setState = (state,callback)=>{ return; }; }

  1. - 方案2:使用标记记录页面的销毁
  2. ```javascript
  3. constructor(props) {
  4. super(props);
  5. this._isMounted = false; // 设立标记
  6. }
  7. componentDidMount() {
  8. this._isMounted = true;
  9. this._isMounted && fetch(`http://localhost:8000/albums`)
  10. .then((response) =>
  11. response.json())
  12. .then((data) => {
  13. this.setState({ album : data });
  14. }).catch((error) => {
  15. console.log("Error " + error)
  16. })
  17. }
  18. componentWillUnmount() {
  19. this._isMounted = false;
  20. }

Antd 时间类组件

  • 禁用某一段时间

    • 禁用日期

      1. limitDay(currentDate: any) {
      2. const today = moment();
      3. return currentDate.isBefore(today, 'day');
      4. }
    • 禁用时间

      1. // desc 限制时间(不得早于当前时间)
      2. limitTime(selected: any) {
      3. const selectDate = selected ? selected : moment();
      4. let disabledHours = 0;
      5. let disabledMinutes = 0;
      6. const selest = selectDate.format('YYYY-MM-DD');
      7. const now = moment().format('YYYY-MM-DD');
      8. if (selest === now) {
      9. const selectHour = selectDate.hour();
      10. const nowHour = moment().hour();
      11. const nowMinute = moment().minute() + 1;
      12. disabledHours = nowHour;
      13. selectHour === nowHour && (disabledMinutes = nowMinute);
      14. }
      15. return {
      16. disabledHours: () => (disabledHours ? range(0, disabledHours) : []),
      17. disabledMinutes: () => (disabledMinutes ? range(0, disabledMinutes) : []),
      18. disabledSeconds: () => [],
      19. };
      20. }

对于无法用Form包裹的组件

  • 有些Form.Item 后面可以通过增加元素实现需求

    1. <Form.Item label="InputNumber">
    2. <div style={{ display: 'flex', flexDirection: 'row' }}>
    3. {getFieldDecorator('input-number', { initialValue: 3 })(<Input style={{ width: 200 }} />)}
    4. <Button>12</Button>
    5. <Button>21221</Button>
    6. </div>
    7. </Form.Item>
  • 无法用form包裹的,可以将其抽离出去,形成一个单独的组件

    1、组件传入一个value 2、在组件中操作、处理逻辑,最终通过 onChange 事件将 value 传递到外部 给 Form Item 使用

  1. class InputWithTips extends Component<IProps, Istate> {
  2. static getDerivedStateFromProps(nextProps: any) { // 重点
  3. // Should be a controlled component.
  4. if ('value' in nextProps) {
  5. return {
  6. number: nextProps.value,
  7. // ...(nextProps.value || {}),
  8. };
  9. }
  10. return null;
  11. }
  12. constructor(props: any) {
  13. super(props);
  14. this.state = {
  15. number: props.value,
  16. }
  17. }
  18. handleChange = (e: any) => {
  19. this.setState({ number: e.target.value })
  20. const { onChange } = this.props; // 重点
  21. if (onChange) {
  22. onChange(e.target.value)
  23. }
  24. }
  25. render() {
  26. const { number } = this.state;
  27. const { tips, addonAfter, style, placeholder, disabled } = this.props;
  28. return (
  29. <React.Fragment>
  30. <Input
  31. style={style}
  32. addonAfter={addonAfter}
  33. placeholder={placeholder}
  34. value={number}
  35. disabled={disabled}
  36. onChange={(e: any) => {
  37. this.handleChange(e)
  38. }}
  39. />
  40. {tips && <span>({tips})</span>}
  41. </React.Fragment>
  42. )
  43. }
  44. }
  45. export default InputWithTips;
  • 不推荐 放弃Form.item 实现的功能,实现一个可控组件, 通过 onChange 等收集数据

实现页面Form表单的方式

  • 编写DOM形式

    1. <Form>
    2. <Form.Item label="模板名称">
    3. {getFieldDecorator('templateName', {
    4. rules: [{ required: true, message: '请输入模板名称' }, { validator: CHECK_Input_Len(20) }],
    5. })(<Input placeholder="请输入模板名称" />)}
    6. </Form.Item>
    7. <Form.Item label="发布内容">
    8. {getFieldDecorator('templateContent', {
    9. rules: [{ required: true, message: '请输入发布内容' }, { validator: CHECK_Input_Len(50) }],
    10. })(<TextArea placeholder="请输入发布内容" autoSize={{ minRows: 3, maxRows: 5 }} />)}
    11. </Form.Item>
    12. </Form>
  • 编写数据,渲染DOM形式

image.pngimage.png

项目中CSS

  • 通过 styled-components ```javascript import styled from ‘styled-components’;

// import { createGlobalStyle } from ‘styled-components’; // 创建全局样式

export const Container = styled.divbackground: #fff; margin: 24px; padding: 24px; minheight: calc(100% - 48px); .ant-form-item:last-child { margin-bottom: 0; };

render(){ xxxx }

  1. - **JSX 中写 style**
  2. ```javascript
  3. <div style={{width: 102, textAlign: 'center'}}>xxx</div>
  • 引入CSS样式表 ```javascript import styled from ‘./style.less’;

render(){

xxxx
}

// 外部的 style.less 样式表 .countDownContainer{ font-size: 14px; font-weight: 400; color: rgba(26, 26, 26, 0.85); line-height: 20px; .paragraph { margin-bottom: 15px; } } ```