reactjs - 如何在反应中切换下拉列表时更新 URL
问题描述
我正在尝试根据下拉列表的值更新 URL。通过什么功能可以更新网址。
handleChange = (e) => {
// update client code
this.setState({ clientcode: e.target.value.substr(0, 5),
value: e.target.value,
});
// now update url
// this.props.history.push('/v4/adminv2/web.php/client-onboard/family-management/api/packet?clientcode=' + this.state.clientcode);
const state = "";
const title = "";
const url = '/v4/adminv2/web.php/client-onboard/family-management/api/packet?clientcode=' + this.state.clientcode;
window.history.pushState(state, title, url);
}
对于下拉列表中的每次更改,都需要更新 URL 中的 this.state.clientcode 并重新加载页面。
下拉值更改后,无法看到 url 的任何更改。
解决方案
您可以使用从路由器传递的历史对象:
handleChange = (e) => {
this.setState({
clientcode: e.target.value.substr(0, 5),
value: e.target.value,
});
const url = `/v4/adminv2/web.php/client-onboard/family-management/api/packet?clientcode=${this.state.clientcode}`
props.history.push(url);
}
这不会重新加载页面
推荐阅读
- c# - 通过在旧版本 Entity Framework 中工作的 Entity Framework Core 执行 GroupBy 查询时引发 NullReferenceException
- html - BS4:用新标签包装一系列兄弟姐妹
- perl - 使用 if 语句时是否可以匹配 perl 中的部分单词(包括 bash 示例)
- html - 将html文件另存为aspx
- oracle - ORACLE SQL 开发人员中的 GHOST 列 SYS_NC00008$ 错误 ORA 00001
- c# - 无法在 Socket Close 上访问已处理的对象
- fortran - 如何使用 MPI 并行化双循环并在内循环中读取文件?
- sql - SQL Server - 在 where 子句中使用 DATALENGTH 会导致截断错误
- python - Python的流式`TarFile`和`os.pipe()`:存档不完整
- vuejs2 - 数据与多次使用的同一组件共享