【问题标题】:React-URLSearchParams returning Null objectReact-URLSearchParams 返回 Null 对象
【发布时间】:2022-01-25 09:32:11
【问题描述】:

我正在使用以下代码从 URL 检索查询参数,但 URLSearchParams 返回一个空对象。

PS:uselocation.search 返回正确的输出。

const stringdata = useLocation().search

const queryparameter = new URLSearchParams(stringdata)
    
console.log("query parameter :", queryparameter)
    
const query = queryparameter.get('q');
    
var url_string = `http://localhost:3000/recipes${query}`

【问题讨论】:

  • 你需要得到一个最小的复制集并发布一个可运行的 sn-p。

标签: reactjs url-parameters


【解决方案1】:

您可以尝试在用户搜索参数上使用 window.location.search。 看起来应该是这样的:

const queryParams = new URLSearchParams(window.location.search);
const query = queryParams.get('q');
let url_string = `http://localhost:3000/recipes${query}`;
        

【讨论】:

    猜你喜欢
    • 2020-12-21
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多