reactjs - 在 react-big-calendar 视图之间切换时,有没有办法让 url 发生变化?
问题描述
我正在尝试使用 react-router-dom 实现 react-big-calendar 视图之间的切换。我知道日历有自己的路由,但是这个路由不会更改 URL,因此用户无法使用箭头或手势返回上一个视图,也无法使用链接打开具体视图。有没有办法实现它?
解决方案
在受控状态场景中,您可以使用 和 with 进行控制date
,onNavigate
您view
可以onView
使用这些方法来控制路由,然后使用您的 url 路由更改更新状态变量。
const onNavigate = (newDate, newView = viewFromState) => {
// convert the date to some url string to use
history.push(`${routeBase}/${convertedDate}/${newView}`);
};
const onView = (newView) => {
history.push(`${routeBase}/${convertedDateFromState}/${newView}`)
};
// at your component route, and this is seriously paraphrasing
const params = useParams();
// I'm using a reducer, since I often update multiple
// bits of state simultaneously. I also, in the reducer,
// remember to convert the 'date' to a true JS Date object
// for RBC
dispatch({type: 'PARAMS', params});
日历使用您的新方法onNavigate
和onView
方法来控制这些状态值,您的方法更新 url 并维护历史记录(以便您可以前进和后退),您的路由器更新日历的实际状态值,您可以获得日历控件和您的浏览器历史记录。
推荐阅读
- python - 如何正确替换字符串中的字符?
- python - python 网络服务器 https 证书
- php - 如何使用 Guzzle 6 获取一些特定的 HTTP 标头而不下载文件?
- r - 如何使用 httr::POST 在 R 中使用标头和数据选项发出 POST 请求?
- php - 如果记录不存在,php 不返回消息:“记录不存在”
- flutter - 如何在 Flutter 中获取系统时间格式(24 或 12 AM/PM)?
- amazon-web-services - 如何从充当客户端的 Lambda 中检索 EC2(服务器)的 IP 地址?
- angular - Angular,行为主体,在一个组件中更改数据,在另一个组件中进行更改
- jquery - 如何在 $(document).ready(function(e){ 中获取文件输入变量
- .htaccess - 将所有 url 索引重写为子文件夹