This video is available to students only

How to Build a Draggable React Column Layout

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

Previous LessonIntroductionNext LessonHow to Bootstrap a React TypeScript App With create-react-app

Lesson Transcript

  • [00:00 - 00:10] Drag column. Let's implement dragging for the column component. Openers see column TSX and define the ref constant that will hold the reference to the dragged div element.

  • [00:11 - 00:26] Constrref = use ref. We specify the type of this ref HTML div element and we pass in the default value null. Import view ref from react. Pass the ref to the column container ref = ref.

  • [00:27 - 00:41] Now we'll use the use item drag hook to find out when did the user begin dragging the column. Constr. Drag = use item drag. Pass in the options type column ID text.

  • [00:42 - 00:57] Import use item drag from the utils and also get the dragged item from the up state. Now the hover event that will use to make the column drop target might be triggered too frequently.

  • [00:58 - 01:06] So we'll use the throttle function from the throttle debounce TS package. Install this package. Yarn add throttle debounce TS.

  • [01:07 - 01:14] After it's installed get the drop function drop. It's the second returned value from the use drop hook.

  • [01:15 - 01:30] So we skip the first one add a comma and then define the drop using use drop and pass the options there. So this hook should accept only columns. We define the hover handler.

  • [01:31 - 01:41] As I said we throttle it 200 milliseconds should be enough and passing the callback. First we check that the dragged item exists. If no dragged item then we return.

  • [01:42 - 02:09] If it exists then we check if the dragged item type is a column then we check if dragged item ID equals ID then we return. Otherwise if it's a column but the idea of our column doesn't match is not equal to drag the item ID then we dispatch move list with dragged item ID and the idea of our column.

  • [02:10 - 02:19] Okay now we combine drag with drop with ref launch the app Yarn start. You should be able to drag the columns.

This lesson preview is part of the Fullstack React with TypeScript Masterclass course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

Unlock This Course

Get unlimited access to Fullstack React with TypeScript Masterclass, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Fullstack React with TypeScript Masterclass