feat: add listen code change
This commit is contained in:
22
src/utils/history.ts
Normal file
22
src/utils/history.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
type To = string | Location;
|
||||
type State<T> = {
|
||||
[key: string]: any;
|
||||
} & T;
|
||||
export const push = <T = any>(to: To, state?: State<T>, refresh = true) => {
|
||||
const _history = window.history;
|
||||
if (typeof to === 'string') {
|
||||
// must key is default, so react navigate can work
|
||||
_history.pushState({ key: 'default', usr: state }, '', to);
|
||||
} else {
|
||||
// const path = to.pathname;
|
||||
_history.pushState({ key: 'default', usr: state }, '', to.pathname);
|
||||
}
|
||||
// must dispatch popstate event, so react navigate can work
|
||||
refresh && window.dispatchEvent(new Event('popstate'));
|
||||
};
|
||||
export const history = {
|
||||
push,
|
||||
};
|
||||
|
||||
// import { createBrowserHistory } from 'history';
|
||||
// export const history = createBrowserHistory();
|
||||
Reference in New Issue
Block a user