之前在v5 中使用的时 withRouter 但是这个方法在 6中废弃
难点: 在类组件中使用 hook: useLocation, useNavigate, useParams
代码:
import React, { Component } from 'react'
import { useLocation, useNavigate, useParams } from 'react-router-dom';
// import './index.css'
class FromCom extends Component {
constructor(props) {
super(props)
this.state = {
submitValue: null
}
}
componentDidMount() {
// setTimeout(()=>{
// this.props.navigate('/pub')
// },2000)
// console.log('llll',this.props)
}
render() {
return (
<div>
</div>
)
}
}
const withRouter = FromCom => props => {
const location = useLocation();
const navigate = useNavigate();
const params = useParams();
return (
<FromCom
{...props}
location={location}
navigate={navigate}
params={params}
/>
);
};
export default withRouter(FromCom);