最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use history using withRouter outside component - Stack Overflow

programmeradmin0浏览0评论

I'm quite new to react router and I'm having few difficulties. I have no problems using history inside a ponent. Howver, I have a function outside the said ponent and it can't detect history. I tried alot of things but to no avail. It gives me a history is undefined error

UserAvatar.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)

any ideas would be of great help Thanks!

I'm quite new to react router and I'm having few difficulties. I have no problems using history inside a ponent. Howver, I have a function outside the said ponent and it can't detect history. I tried alot of things but to no avail. It gives me a history is undefined error

UserAvatar.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)

any ideas would be of great help Thanks!

Share Improve this question asked Jul 5, 2018 at 13:38 Jason Jason 2818 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You can use the history library to manually create the history outside of your ponent tree and give that to your Router ponent. This way you can import the history object wherever you need it.

Example

// history.js
import { createBrowserHistory } from 'history';

export default createBrowserHistory();

// index.js
import { Router, Route, Link } from 'react-router-dom';
import history from './history';

ReactDOM.render(
  <Router history={history}>
    <div>
      <ul>
        <li><Link to="/">Home</Link></li>
        <li><Link to="/user">User</Link></li>
      </ul>
      <Route exact path="/" ponent={HomePage} />
      <Route path="/user" ponent={UserAvatar} />
    </div>
  </Router>,
  document.getElementById('root')
);

Issue is with signout function definition. You are making history as local variable to your function. Remove param history from signout method. this should help atleast to print it correctly.

const signOut = () => { // remove hestory from hear as its a globally available.

    console.log(history);
    localStorage.removeItem("token");
    localStorage.removeItem("user");
    history.replace('/sign-in');
};

you can use this package history

import it in your file import { createBrowserHistory } from 'history'; then use it like this

createBrowserHistory().push('/'); then you can reload the dom window.location.reload();`

发布评论

评论列表(0)

  1. 暂无评论