resizable tables can be a problem to implement but I'm going to show you how to do them like a Chad the first thing we want to do is initialize Ved project with react CD into it and install its dependencies we're going to use Tailwind for styling and react resizable react resizable offers us abstractions so that we can resize anything that's wrapped with the resizable component we use this because we don't want to implement our own Logic for it so we're going to install tailwind and set it up we're going to add the path to our
content and add the Imports now add react resizable and add the types for it we're going to create interface constants and hooks folders and we're going to go to our TS config file and set up relative paths now that we have our project setup we can go to the interfaces folder create an index.ts and a column. TS files column. DS file is going to be used to represent what the ACT actual table column will contain it has a name which is used for internal tracking display name which is going to be shown to the user
and the actual width in pixels now we need to Define how our table will look like meaning what columns will the table have and that's why we will create a table columns file and an index file inside the constants folder now our table will have three columns first name last name and age now finally we will create a host hook called use columns that will bind all of this together this hook will manage the internal State and it will track the column widths the Ed column hooks takes in initial columns and a default width will
create a handle resize function that finds the existing column by its name and updates the width get column width is a function that will take the name of the column and return its width now we can return all of these functions that we defined and we can export the Ed columns hook inside the index XTS file now we can use the use columns hook and pass it the columns constant that we declared and also pass 250 as a default withth now we will add table table head and a table row tag and inside of the
table row we will map over the columns now each column will return a resizable component which has the handle which takes jsx and it's a handle that will be positioned to the right of the column which we will use to resize the actual table column it has a width which which is return from the used column so and we also have the on resize event this event triggers each time we move or resize the column and it will update the internal state of our used columns hook now let's add some sample data and let's see
what we made now as you can see this table has handles on the right side it can be fully resizable and it can be moved to a Pixel Perfect point if you want to have different starting widths for the columns you can just update them in your data and as you can see changing the WID wi actually reflects the widths in the table and there you go now you know how to implement resizable tables in react this is a new type of video that I'm making so please give me feedback in the comments take care