React 抛出 URIError:这可能是由无效的百分比编码引起的

Sta*_*tas 6 router urlencode reactjs

正在使用react,然后遇到了这样的问题

  Uncaught URIError: This is likely caused by an invalid percent-encoding
Run Code Online (Sandbox Code Playgroud)

我目前正在使用新闻 API,一些文章可能包括%. 我的整个应用程序依赖于在网址中显示新闻文章名称,因为我使用this.props.match.params.id

我尝试在网上寻找解决方案,但大多数人都不清楚如何解决这个确切的问题。

有一个简单的解决方法吗?

小智 3

您需要使用encodeURIComponent()收到的路径作为参数:示例:

const receivedArticleName = encodeURIComponent('Article Name with %');
Run Code Online (Sandbox Code Playgroud)

由于您正在使用 API,因此一旦收到它,请使用收到的ArticleName 设置您的 URL 变量,然后就完成了。