Open In App

ReactJS UI Ant Design Transfer Component

Ant Design Library has this component pre-built, and it is very easy to integrate as well. Transfer Component is used for the double column transfer choice box. It is used to transfer the items between two columns effectively. We can use the following approach in ReactJS to use the Ant Design Transfer Component.

Transfer Props:



Render Props:



 

Creating React Application And Installing Module:

Project Structure: It will look like the following.

Project Structure

Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.




import React, { useState } from 'react';
import "antd/dist/antd.css";
import { Transfer } from 'antd';
  
// Our sample Mock Data
const mockData = [
  {key: "0", title: "Title 0", description: "Sample Description 0"}, 
  {key: "1", title: "Title 1", description: "Sample Description 1"},
  {key: "2", title: "Title 2", description: "Sample Description 2"},
  {key: "3", title: "Title 3", description: "Sample Description 3"},
  {key: "4", title: "Title 4", description: "Sample Description 4"},
  {key: "5", title: "Title 5", description: "Sample Description 5"},
];
  
export default function App() {
  
  // To set target keys
  const [targetKeys, setTargetKeys] = useState(mockData);
    
  // Contains the selected keys
  const [selectedKeys, setSelectedKeys] = useState([]);
  
  return (
    <div style={{
      display: 'block', width: 700, padding: 30
    }}>
      <h4>ReactJS Ant-Design Transfer Component</h4>
      <Transfer
        dataSource={mockData}
        titles={['Source', 'Target']}
        render={item => item.title}
        selectedKeys={selectedKeys}
        targetKeys={targetKeys}
        onChange={(nextTargetKeys) => {
          setTargetKeys(nextTargetKeys);
        }}
        onSelectChange={(sourceSelectedKeys, targetSelectedKeys) => {
          setSelectedKeys([...sourceSelectedKeys, ...targetSelectedKeys]);
        }}
      />
    </div>
  );
}

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Reference: https://ant.design/components/transfer/


Article Tags :