我们可以安全地使用react-router的私有方法吗? [已关闭]

问题描述 投票:0回答:1

我想访问React之外的

navigate
函数,我读了这个问题并得到了以下答案,它有效,但我发现在这个函数的注释中写着:
@internal PRIVATE - DO NOT USE
,我可以使用这个吗安全运行吗?谢谢!

const router = createBrowserRouter([]);

router.navigate("/"); // is this safe?
react-router react-router-dom
1个回答
0
投票

似乎采用 delta

navigate
类型的
number
函数被标记为内部。

参见router.ts来源:

/**
 * @internal
 * PRIVATE - DO NOT USE
 *
 * Navigate forward/backward in the history stack
 * @param to Delta to move in the history stack
 */
navigate(to: number): Promise<void>;

/**
 * Navigate to the given path
 * @param to Path to navigate to
 * @param opts Navigation options (method, submission, etc.)
 */
navigate(to: To | null, opts?: RouterNavigateOptions): Promise<void>;

虽然我怀疑这实际上是维护者的疏忽,旨在将两个变体明确标记为内部。

@internal PRIVATE - DO NOT USE
,我可以安全地使用这个功能吗?

React-Router/React-Router-DOM 在呈现“内部”函数/功能/等方面有着悠久的历史,通常伴随着类似于以下内容的注释:

///////////////////////////////////////////////////////////////////////////////
// DANGER! PLEASE READ ME!
// We provide these exports as an escape hatch in the event that you need any
// routing data that we don't provide an explicit API for. With that said, we
// want to cover your use case if we can, so if you feel the need to use these
// we want to hear from you. Let us know what you're building and we'll do our
// best to make sure we can support you!
//
// We consider these exports an implementation detail and do not guarantee
// against any breaking changes, regardless of the semver release. Use with
// extreme caution and only if you understand the consequences. Godspeed.
///////////////////////////////////////////////////////////////////////////////

换句话说,他们并不“官方”支持它,但仍然试图提供帮助。 “非官方”导出是完全可以安全使用的,直到它们不再可以安全使用,即维护者最终决定将其从库中删除。 关于

router.navigate(...)

,这是截至 2022 年 11 月 2 日的官方建议,并且截至 2023 年 3 月 28 日

仍然是推荐的方法,并且在 Github 问题线程结束时立场没有改变。 
© www.soinside.com 2019 - 2024. All rights reserved.