react+ts关于history组件
`
// 安装:yarn add history
// 创建 router/history.js 文件
// 在该文件中,创建一个 hisotry 对象并导出
// 携带参数
// 在入口index.js 中导入 history 对象,并设置为 Router 的 history
// 通过响应拦截器处理 token 失效
import { useState, useLayoutEffect, FC } from 'react'
import { createBrowserHistory, createHashHistory } from 'history'
import { HistoryRouterProps, Router } from 'react-router-dom'
export const history = createBrowserHistory()
export const HistoryRouter: FC
children,
history,
}) => {
const [state, setState] = useState({
action: history.action,
location: history.location,
})
useLayoutEffect(() => {
history.listen(setState)
}, [history])
return <Router children={children} navigator={history} {...state} />
}
`
本文来自博客园,作者:jialiangzai,转载请注明原文链接:https://www.cnblogs.com/zsnhweb/articles/16879060.html

浙公网安备 33010602011771号