如何将基于 ANTD class 的组件转换为基于函数的组件

How to convern ANTD class-based component to react function-based component

任何人都可以就如何将基于 Ant Design class 的组件转换为基于函数的组件提出建议吗?我是基于 class 的新手,所以转换 class 组件让我感到很困惑。现在,我的应用程序是一个基于功能的组件。任何建议将不胜感激!

传输组件

import { Transfer, Button } from 'antd';

class App extends React.Component {
  state = {
    mockData: [],
    targetKeys: [],
  };

  componentDidMount() {
    this.getMock();
  }

  getMock = () => {
    const targetKeys = [];
    const mockData = [];
    for (let i = 0; i < 20; i++) {
      const data = {
        key: i.toString(),
        title: `content${i + 1}`,
        description: `description of content${i + 1}`,
        chosen: Math.random() * 2 > 1,
      };
      if (data.chosen) {
        targetKeys.push(data.key);
      }
      mockData.push(data);
    }
    this.setState({ mockData, targetKeys });
  };

  handleChange = targetKeys => {
    this.setState({ targetKeys });
  };

  renderFooter = (props, { direction }) => {
    if (direction === 'left') {
      return (
        <Button size="small" style={{ float: 'left', margin: 5 }} onClick={this.getMock}>
          Left button reload
        </Button>
      );
    }
    return (
      <Button size="small" style={{ float: 'right', margin: 5 }} onClick={this.getMock}>
        Right button reload
      </Button>
    );
  };

  render() {
    return (
      <Transfer
        dataSource={this.state.mockData}
        showSearch
        listStyle={{
          width: 250,
          height: 300,
        }}
        operations={['to right', 'to left']}
        targetKeys={this.state.targetKeys}
        onChange={this.handleChange}
        render={item => `${item.title}-${item.description}`}
        footer={this.renderFooter}
      />
    );
  }
}

ReactDOM.render(<App />, mountNode);

在函数组件中定义 state 你可以使用 useState 钩子,也可以使用 useEffect 钩子代替 componentDidMount, componentDidUpdate, ... 等生命周期,比如:

import {useState, useEffect} from 'react';
import { Transfer, Button } from 'antd';

function App() {
  const [state, setState] = useState({
    mockData: [],
    targetKeys: [],
  });

  const getMock = () => {
    const targetKeys = [];
    const mockData = [];
    for (let i = 0; i < 20; i++) {
      const data = {
        key: i.toString(),
        title: `content${i + 1}`,
        description: `description of content${i + 1}`,
        chosen: Math.random() * 2 > 1,
      };
      if (data.chosen) {
        targetKeys.push(data.key);
      }
      mockData.push(data);
    }
    setState({ mockData, targetKeys });
  };

  useEffect(()=>{
    getMock();
  }, [])

  const handleChange = targetKeys => {
    setState(prevState => ({ ...prevState, targetKeys }));
  };

  const renderFooter = (props, { direction }) => {
    if (direction === 'left') {
      return (
        <Button size="small" style={{ float: 'left', margin: 5 }} onClick={getMock}>
          Left button reload
        </Button>
      );
    }
    return (
      <Button size="small" style={{ float: 'right', margin: 5 }} onClick={getMock}>
        Right button reload
      </Button>
    );
  };

  return (
    <Transfer
      dataSource={state.mockData}
      showSearch
      listStyle={{
        width: 250,
        height: 300,
      }}
      operations={['to right', 'to left']}
      targetKeys={state.targetKeys}
      onChange={handleChange}
      render={item => `${item.title}-${item.description}`}
      footer={renderFooter}
    />
  );
}