react-router withRouter不提供mapStateToProps中的道具?

时间:2017-04-01 14:12:05

标签: reactjs react-router

我目前面临一个奇怪的问题,app.service("DataService", function() { console.log("DataService"); var localProfile = JSON.parse(localStorage.getItem("profile")); var profiles = JSON.parse(localStorage.getItem("profile")); if (localProfile != undefined && localProfile.length > 0) { this.profiles = localProfile; } else { this.profiles = [{ id: "1526", username: "berserk", password: "berserk", age: "31" }, { id: "1358", username: "Johnathan", password: "test", age: "17" }, { id: "2539", username: "Britney", password: "test", age: "18" }, { id: "1486", username: "Kevin", password: "test", age: "7" }]; } this.getProfile = function() { return this.profiles; } }); 提供的HOC withRouter没有将道具传递给mapStateToProps函数。我在这做错了吗?

react-router

2 个答案:

答案 0 :(得分:10)

您必须在连接组件之前注入路径

要实现这一点,你必须使用

export default withRouter(connect(mapStateToProps)(ThisClass));

而不是

export default connect(mapStateToProps)(withRouter(ThisClass));

答案 1 :(得分:0)

对我来说,我试图获取原始状态对象,为此我在子类上使用了mapStateToProps。我正在使用React-Redux,这对我有用。

var connect = require('react-redux').connect;
var HeaderSection = React.createClass({
  render: function() {
   console.log(this.props);
  }
});

function mapStateToProps(state) {
  return state;
}

module.exports = connect(mapStateToProps)(HeaderSection);