0

使用@reach/router 时如何设置搜索参数?我只是使用了 props.history.push 但 props.history 仍然未定义。这是页面状态更改时的代码

const handleChangePage = page => {
    setPage(page)
    const query = new URLSearchParams(props.location.pathname)
    props.history.push({
       pathname: props.location.pathname,
       search: query.toString(),
    })
    //???  I want to set page for url similar to /product?page=3&limit=4
}
4

1 回答 1

1

使用反应路由器,您必须使用它navigate来更改路由,而不是history

你也可以props.location.search用来得到query params

const handleChangePage = page => {
    setPage(page)
    const query = new URLSearchParams(props.location.pathname)
    const path = `${props.location.pathname}?${query.toString()}
    navigate(path);
}
于 2020-05-22T13:35:26.407 回答