使用上面的答案,我发布了完整的代码,以便更多的用户可以理解这一点。
应用程序.js 文件
class App extends React.Component {
constructor(props) {
super(props);
this.state = {open: false};
this.state = {message: "StackOverflow"};
}
return (
<Router>
<div>
<AppBar title="App" onLeftIconButtonTouchTap={this.handleToggle} />
<Drawer containerStyle={{height: 'calc(100% - 64px)', top: 64}} docked={true} width={200} open={this.state.open} zDepth={2}>
<Link to="/dashboard" style={{ textDecoration: 'none' }}><MenuItem>Dashboard</MenuItem></Link>
<Link to="/information" style={{ textDecoration: 'none' }}><MenuItem>Information</MenuItem></Link>
</Drawer>
<Route exact path="/dashboard" render={props => <Dashboard someProp={this.state.message} {...props} />} />
<Route exact path="/information" render={props => <Information someProp={this.state.message} {...props} />} />
</div>
</Router>
);
}
仪表板.js
import React from 'react';
class Dashboard extends React.Component {
constructor(props) {
super(props);
}
render() {
console.log(this.props);
const {styleFromProps} = this.props;
const contentStyle = { ...styleFromProps, transition: 'margin-left 450ms cubic-bezier(0.23, 1, 0.32, 1)' };
return (
<div style={contentStyle}><h1> Hello {this.props.someProp}</h1></div>
);
}
}
export default Dashboard;