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});
1条答案
按热度按时间pengsaosao1#
在受控状态场景中,您可以使用
onNavigate
控制date
,使用onView
控制view
,您可以使用这些方法来控制路由,然后使用url路由更改来更新状态变量。Calendar使用新的
onNavigate
和onView
方法来控制这些状态值,您的方法更新url并维护历史记录(这样您就可以前进和后退),您的Router更新Calendar的实际状态值,并且您可以同时获得日历控件和浏览器历史记录。