0
注意:这与我的问题无关,但我已阅读this link。是否有可能将上下文传递给React中的哑元子组件?
说我有我的应用程序组件:
import React, { Component, PropTypes } from 'react';
class App extends Component {
// stuff & code
<Header />
// code & stuff
}
App.childContextTypes {
router: Proptypes.object.isRequired;
}
然后,我有我定义为一个常数其页眉部分:
import React, { PropTypes } from 'react';
getPath() {
const router = { this.context };
return router.location.pathname;
}
const Header = (props) => {
// header stuff
<div>{getPath()}</div>
}
Header.contextTypes {
router: Proptypes.object.isRequired;
}
所有上面是假的代码,但它的我简而言之就是。到目前为止,当我尝试访问标题中的路由器对象时,router
对象是undefined
。是否有可能通过道具以这种方式传递给const
的组件并且没有明确定义为React类?
在我的反应项目中,我使用了react-router。