Blog>
Snippets

Setting Up TanStack Router for Data Mutation

Demonstrate the initial setup of TanStack Router in a JavaScript application, including the installation and basic route configuration for a CRUD operation.
npm install @tanstack/react-location
First, install the TanStack Router library (known here as React Location for routing).
import { ReactLocation, Router } from '@tanstack/react-location';

const location = new ReactLocation();
Import ReactLocation and Router from the TanStack Router package and create a new instance of ReactLocation.
const routes = [
  {
    path: '/',
    element: () => <div>Home Page</div>,
  },
  {
    path: 'create',
    element: () => <div>Create Page</div>,
  },
  {
    path: 'update/:id',
    element: ({ params }) => <div>Update Page for {params.id}</div>,
  },
  {
    path: 'delete/:id',
    async resolve({ params }) {
      await deleteItem(params.id);
      return () => <div>Item Deleted</div>;
    }
  }
];
Define your routes including paths for CRUD operations. Here, 'create', 'update/:id', and an asynchronous 'delete' route are setup.
function App() {
  return <Router location={location} routes={routes} />;
}
Finally, setup your App component to use the Router with the defined location and routes.