Skip to content

React router generate path patterns / React router 根据路由表生成全部的 patterns #271

@JiangWeixian

Description

@JiangWeixian

Based on react-router@v6

function generatePaths(route: RouteObject, parentPath = '') {
  const { path, children } = route
  const fullPath = cleanDoubleSlashes(withoutTrailingSlash(`${parentPath}/${path ?? ''}`))

  let paths = new Set([fullPath])

  if (children) {
    for (const childRoute of children) {
      const childPaths = generatePaths(childRoute, fullPath)
      paths = new Set([...paths, ...childPaths])
    }
  }

  return paths
}

// Paths should be uniq
const paths = routes.flatMap(route => Array.from(generatePaths(route)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions