我正在使用反应路由器 1.0.2,我的路线如下所示:
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="triangles" component={Triangles}/>
</Route>
</Router>
</Provider>,
document.querySelector('.container')
);
我的 App 组件看起来像这样,我想我可以在道具中传递位置:
import React, {Component} from 'react';
import Menu from './menu';
export default class App extends Component {
render() {
return (
<div>
<Menu/>
<div className="jumbotron">
{this.props.children && React.cloneElement(this.props.children, {
location: this.props.location
})}
</div>
</div>
);
}
};
我想有条件地在 Menu 组件上设置一个活动类:
import React, {Component} from 'react';
import { pushPath } from 'redux-simple-router';
import { Link } from 'react-router';
export default class Menu extends Component {
render() {
return (
<nav role="navigation" className="navbar navbar-default">
<div id="navbarCollapse" className="collapse navbar-collapse">
<ul className="nav navbar-nav">
<li className={this.props.location.pathname === '/' ? 'active' : ''}>
<Link to="/">Home</Link>
</li>
</ul>
</div>
</nav>
);
}
};
但是this.props.location
当菜单的渲染函数被调用时是 null 吗?
如何将道具传递给子组件?