Socket
Socket
Sign inDemoInstall

@tanstack/react-router

Package Overview
Dependencies
Maintainers
2
Versions
576
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tanstack/react-router


Version published
Weekly downloads
143K
decreased by-18.06%
Maintainers
2
Weekly downloads
 
Created

What is @tanstack/react-router?

@tanstack/react-router is a powerful and flexible routing library for React applications. It provides a comprehensive set of features for managing navigation, route matching, and data fetching in a declarative manner.

What are @tanstack/react-router's main functionalities?

Basic Routing

This feature allows you to define basic routes for your application. The `Router` component takes an array of route objects, each specifying a path and the component to render.

import { Router, Route } from '@tanstack/react-router';

const routes = [
  { path: '/', element: <Home /> },
  { path: '/about', element: <About /> },
];

function App() {
  return (
    <Router routes={routes} />
  );
}

Nested Routes

Nested routes allow you to define routes within routes, enabling more complex and hierarchical navigation structures. The `children` property is used to specify nested routes.

import { Router, Route } from '@tanstack/react-router';

const routes = [
  { path: '/', element: <Home />, children: [
    { path: 'profile', element: <Profile /> },
    { path: 'settings', element: <Settings /> },
  ]},
];

function App() {
  return (
    <Router routes={routes} />
  );
}

Route Parameters

Route parameters allow you to capture dynamic values from the URL. The `useParams` hook is used to access these parameters within your component.

import { Router, Route, useParams } from '@tanstack/react-router';

const routes = [
  { path: '/user/:id', element: <User /> },
];

function User() {
  const { id } = useParams();
  return <div>User ID: {id}</div>;
}

function App() {
  return (
    <Router routes={routes} />
  );
}

Data Fetching

This feature allows you to fetch data before rendering a route. The `loader` function is used to fetch data, and the `useLoaderData` hook is used to access the fetched data within your component.

import { Router, Route, useLoaderData } from '@tanstack/react-router';

const routes = [
  { path: '/data', element: <DataComponent />, loader: fetchData },
];

async function fetchData() {
  const response = await fetch('/api/data');
  return response.json();
}

function DataComponent() {
  const data = useLoaderData();
  return <div>Data: {JSON.stringify(data)}</div>;
}

function App() {
  return (
    <Router routes={routes} />
  );
}

Other packages similar to @tanstack/react-router

Keywords

FAQs

Package last updated on 22 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc