Code Monkey home page Code Monkey logo

react-router-infer's Introduction

react-router-infer

ci

A little type-safer react-router-dom. Inspired by @tanstack/router.

What's covered:

Prerequisites

  • typescript >= 5.3. This allows r helper or [...] as const sastisfies RouteObject[] to work. See this PR.
  • react-router-dom >= 6.0.0. For Outlet, RouteObject.

Quick start

Install packages:

npm install react-router-infer react-router-dom

Setup your routes:

// 1. Define routes with `r` function. `parseSearch` is added to type URL search params.
const routes = r([
  {
    path: '/',
    children: [
      {
        path: ':id',
        parseSearch: (jsonObject) =>
          z.object({ page: z.number().catch(1) }).parse(jsonObject),
      },
    ],
  },
])

// 2 Register root route:
declare module 'react-router-infer' {
  interface Register {
    routes: CreateRoutes<typeof routes>
  }
}

// 3. Wrap root route with `withSearchParamsProvider` before passing it to `createBrowserRouter` or `useRoutes`:
const router = createBrowserRouter(
  withSearchParamsProvider({
    routes,
    /**
     * Optionally customize search params stringify and parse functions
     *
     * @see https://tanstack.com/router/v1/docs/guide/custom-search-param-serialization
     */
    // stringifySearch,
    // parseSearch
  }),
)

// 4. Use hooks/components from `react-router-infer`:

// useParams
const { id } = useParams({ from: '/:id' }) // from is optional for useParams

// useNavigate/Link
const navigate = useNavigate()
navigate({
  to: '/:id',
  params: {
    id: '123',
  },
  search: {
    page: 1,
  },
})

// useSearch
const { search, setSearch, isInvalidRoute } = useSearch({
  from: '/:id',
})
// search/setSearch can be undefined
if (isInvalidRoute)
  throw new Error('useSearch is being called out of "/:id" route')
// search/setSearch is defined after checking isInvalidRoute
setSearch({
  page: search.page + 1,
})

API & Example

See the docs or the example app

react-router-infer's People

Contributors

renovate[bot] avatar haivuw avatar

Watchers

 avatar

react-router-infer's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

bun
package.json
  • @happy-dom/global-registrator ^13.2.0
  • @size-limit/preset-small-lib ^11.0.2
  • @testing-library/react ^14.1.2
  • @types/react ^18.2.48
  • @typescript-eslint/eslint-plugin ^6.19.0
  • @typescript-eslint/parser ^6.19.0
  • eslint ^8.56.0
  • eslint-plugin-react-hooks ^4.6.0
  • prettier ^3.2.4
  • size-limit ^11.0.2
  • tsup ^8.0.1
  • typescript ^5.3.3
  • zod ^3.22.4
example/package.json
  • axios ^1.6.5
  • axios-cache-interceptor ^1.5.1
  • pokenode-ts ^1.20.0
  • react-dom ^18.2.0
  • swr ^2.2.4
  • valibot ^0.26.0
  • zod ^3.22.4
  • @types/react ^18.2.48
  • @types/react-dom ^18.2.18
  • @typescript-eslint/eslint-plugin ^6.19.0
  • @typescript-eslint/parser ^6.19.0
  • @vitejs/plugin-react ^4.2.1
  • autoprefixer ^10.4.17
  • eslint ^8.56.0
  • eslint-plugin-react-hooks ^4.6.0
  • eslint-plugin-react-refresh ^0.4.5
  • postcss ^8.4.33
  • tailwindcss ^3.4.1
  • typescript ^5.3.3
  • vite ^5.0.12
github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • oven-sh/setup-bun v1

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.