我对react-router很陌生,我遇到的困难很少。我在组件内使用历史记录没有问题。但是,我在上述组件之外有一个函数,它无法检测历史记录。我尝试了很多东西,但无济于事。它给了我一个history is undefined
错误
用户头像.js
import {withRouter} from "react-router-dom";
const signOut = (history) => {
console.log(history);
localStorage.removeItem("token");
localStorage.removeItem("user");
history.replace('/sign-in');
};
export class UserAvatar extends Component {
render() {
const content = (
<div>
<p>Content</p>
<Button className="sign-out" onClick={() => signOut(this.props.history)}>Sign-out</Button>
</div>
);
export default withRouter(UserAvatar, signOut)
任何想法都会有很大帮助谢谢!