React-router-dom v6 recursive, nested routing

Viewed 42

I have some data which has a structure of tree but sometimes it gets update from backend side how can I use this tree recursively I tried this kind of structure but react router dom don't let me to create this kind structure, for example : I want to go /a , /a/b , /a/c , /a/b/d

this is the sample route tree which I want to implement but ignore the elements I just created which I want show you.

<Route path='a' element={<Thing item={a} />}>
  <Route path='b' element={<Category item={b} />}>
    <Route path='d' element={<Content item={d} >}/>
  </Route>
  <Route path='c' element={<Category item={c} >}/>
</Route>

example mapping

 {legislationsData?.map((item) => (
          <React.Fragment key={item.Id}>
            <Route
              path={item.Path}
              element={item.Children?.length > 0 ? <Category Children={item.Children} /> : <ContentList item={item} />}
            />
            {item?.Children?.map((sub) => (
              <React.Fragment key={sub.Id}>
                <Route path={item.Path + sub.Path} element={<ContentList item={sub} />} />
                {navigationDatas.map((nav) => (
                  <Route
                    key={nav.Id}
                    path={item.Path + sub.Path + nav.Path}
                    element={<ContentDetail item={nav} />}
                  />
                ))}
              </React.Fragment>
            ))}
          </React.Fragment>
        ))}

This is the sample data which I am working on it:

"Data": [
    {
      "Id": "1",
      "ParentId": "0",
      "Name": "Genelge",
      "Path": "genelge",
      "Children": [
        {
          "Id": "2",
          "ParentId": "1",
          "Name": "General",
          "Path": "general",
          "Children": []
        },
        {
          "Id": "3",
          "ParentId": "1",
          "Name": "External",
          "Path": "external",
          "Children": [
            {
              "Id": "4",
              "ParentId": "3",
              "Name": "Terminal"
              "Path": "terminal",
            }
          ]
        }
      ]
    },
    {
      "Id": "5",
      "ParentId": "0",
      "Name": "law",
      "Path": "law",
      "Children": []
    },
    {
      "Id": "6",
      "ParentId": "0",
      "Name": "Ithalat Things",
      "Path": "ithalat-things",
      "Children": [
        {
          "Id": "7",
          "ParentId": "6",
          "Name": "KTO",
          "Path": "kto",
          "Children": []
        },
        {
          "Id": "8",
          "ParentId": "6",
          "Name": "Genelgeler",
          "Path": "genelgeler",
          "Children": []
        }
      ]
    }
  ],
2 Answers
Related