Tencent / Tencent/tdesign-react
[Breadcrumb] 组件 BreadcrumbItem 的 router 和 to 属性不适用于 React
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 965
- Forks
- 373
- Avg merge
- 3d 6h
- Merged PRs (30d)
- 17
Description
tdesign-react 版本
1.5.5
重现链接
https://codesandbox.io/p/sandbox/tdesign-react-breadcrumb-router-v6-krkvtm
重现步骤
组件 BreadcrumbItem 的 router 和 to 属性仅适用于 Vue,而且 to 的格式完全是按照 Vue Router 的格式来的(文档)。
实际上这两个属性在 React 中很难用得上,毕竟 React 没有类似于 “Router 对象” 这种东西;就算 Next.js 里的 useRouter() 也和这个组件不兼容。
参考 Demo:https://codesandbox.io/p/sandbox/tdesign-react-breadcrumb-router-v5-vgc6tt
react-router-dom 的 v5 版本,可以使用 useHistory() 获取一个对象(文档),它可以作为 router 参数传,它有 .push(path, [state]) 方法和 .replace(path, [state]) 方法,正好可以兼容此组件,但此时 to 属性也只能支持字符串了。
参考 Demo:https://codesandbox.io/p/sandbox/tdesign-react-breadcrumb-router-v6-krkvtm
react-router-dom 的 v6 版本开始,删去了 useHistory(),而使用 createBrowserRouter() 时会返回一个对象,这个对象确实可以被看作是 “Router 对象”,但它的 API 又完全不一样了:.navigate(path) 等同于组件源码里的 .push(path),而 .navigate(path, { replace: true }) 等同组件源码里的 .replace(path)。
期望结果
如果想兼容 react-router-dom@5,to 属性建议改成仅允许传字符串(可以不考虑向前兼容,因为之前的对象用法一定会报错);
如果想兼容 react-router-dom@6,源码里的 .push() 和 .replace() 需要按照上面的 API 格式改成 .navigate(...) 的写法,为了兼容性,可能还需要加个判断条件 if(router.navigate),此时 to 属性也建议只允许字符串。
实际结果
No response
框架版本
无关
浏览器版本
无关
系统版本
无关
Node版本
无关
补充说明
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the BreadcrumbItem implementation that handles the router and to props, then reproduce the behavior using the linked react-router-dom v5 and v6 CodeSandbox examples. Determine the supported router API and string to format, and consider both compatibility paths described in the issue. Done means the selected React Router usage works without the Vue-specific object format.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100