Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JiangWeixian opened this issue Sep 3, 2023 · 0 comments
Labels
🌐 react tips about react

Comments

@JiangWeixian
Copy link
Owner

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)))
@JiangWeixian JiangWeixian added the 🌐 react tips about react label Sep 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌐 react tips about react
Projects
None yet
Development

No branches or pull requests

1 participant