ReactJS this.props.history.push不存在! this.props不包括历史

问题描述 投票:2回答:1

在App.js内部我管理路由(下面)。我在这两个班级

import {browserHistory} from "react-router";

当我点击图像时,控制台显示错误无法读取未定义的属性“推送”。所以我的道具不包括历史对象。如何使用browserHistory重定向用户?

////

    <Router history={browserHistory}>
    <div>
    <Route path="/profile" component={MyProfile} />
    </div
    </Router>

/// The code above is in a different class

    myprofile(props) {
        this.props.history.push('/profile');
    }

    return (
    <React.Fragment>
    <img src="../images/my-profile.png" alt="profile" className="myprofile" id="myprofile" onClick={this.myprofile.bind(this)} />
    </React.Fragment>
    );
javascript reactjs redirect missing-data browser-history
1个回答
0
投票

如果您尝试使用路径中未定义的组件中的方法this.history.push,则需要将所有路由器包装在“BrowserRouter”组件中,而不是使用“withRouter(ComponentName)”包装该组件。 。

© www.soinside.com 2019 - 2024. All rights reserved.