Effortless Row Deletion - Tanstack Table (shadcn UI version)
Tags: react, javascript, programming, tutorial
Published: 2023-08-14 18:44:53

In the world of UI design, we're often drawn to the allure of sleek interfaces and smooth interactions. [Shadcn](https://ui.shadcn.com/docs/components/data-table) UI, a favorite among many, has made its mark in the realm of visual aesthetics.

I have been having a blast using it in my project building dashboards. For my table needs, I turned to the tried-and-true code from the [Task Example](https://ui.shadcn.com/examples/tasks).

## How to delete a row or rows from the table?
`The challenge was clear: I wanted the power to delete rows with minimal clutter. The solution lay in simplicity – a single button capable of removing one or more rows at once. Whether you're following the Task Example or charting your own course, the method remains as accessible as ever.`

If you are also following the code structure from the above task example, you can follow the example below even if you're not, you can use twerk to meet your requirement. [shadcn code repo](https://github.com/shadcn-ui/ui/tree/main/apps/www/app/examples/tasks/components)

---

<u>

### Step 1: The Magical Prop
</u>

First, we must pass a prop to the data-table component – a state setter function. Think of it as a conduit for gathering selected row data. Imagine, with just a few lines of code, we could have this powerful ability at our fingertips.

![react code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqzilrzd5i7q91x2i450.png)

pass the selected row data to the state setter function using useEffect(). 

![react code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gkfku6iz7f8l1drslvtn.png)

> In the example above, I extracted the _id (an essential piece of information for my MongoDB needs), but feel free to tailor the code to your unique requirements.
<u>



### Step 2: An Invitation to the Future
</u>
Let's continue our journey by passing this prop from the parent component. In the heart of your application, where the "DataTable" is imported, you'll find yourself initializing a state `const [selectedRows, setSelectedRows] = useState([]) `to house the selected rows' data. This state's setter function will become your guiding light.

![component code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bno8gz4v9tt2xvrcne4i.png)

### Step 3: The Grand Reveal
As you tie the pieces together, you'll witness the transformation before your eyes. Rows selected with a click, a button's gentle promise of deletion, and the streamlining of your user experience. With a touch of elegance, your interface embraces efficiency and clarity.

![delete row gif](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExejB2bzRmeXQwNzUweHNyemRsbzdzdmV0c2loaTdhNTh1eHZub29ubiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/GGccFkKj7jDjsWKQlp/giphy.gif)

---


### Conclusion
As you delve into the world of streamlined table interactions, I hope this guide proves to be a guiding light for you. Embrace the elegance of the tanstack table's hidden prowess – the ability to delete rows effortlessly with a single click. Just as I've shared my insights, I invite you to contribute your own wisdom. If you're privy to even more effective methods for row deletion, don't hesitate to share in the comments below. After all, the journey of learning and growth is one we embark upon together, and every insight adds to our collective mastery. Happy coding, and let's keep exploring new horizons!

---