一.state格式
history.push(path, [state])
<Redirect to={{pathname: '/xxx', state: {} }}
二.实际操作
1.用history.push方法携带state参数
history.push('/login', { from: history.location.pathname })
2.用Redirect里的to属性携带state参数
<Redirect to={{pathname: '/login',state: { from: history.location.pathname }}}/>
3.在login页面查看传过来的state参数
import { useHistory } from 'react-router'
const Login = () => {
const history = useHistory()
console.log(history, 'history')
}
4.跳转回原页面
history.replace(history.location.state.from || '/home')