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

I'm quite new to react router and I'm having few difficulties. I have no problems using histo

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();`

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742398079a4436349.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信