【发布时间】:2025-11-29 22:30:01
【问题描述】:
我是新手,目前我被困在一个我有 2 个组件的场景中,
- 启动器组件
- 开发者组件
在此,Launcher 组件首先被调用。从按钮单击它路由到 Developer 组件。我想在路由之后将一个值从 Developer 组件传回 Launcher 组件。
Launcher.js
class LauncherApp extends Component {
constructor(props) {
super(props);
this.developerMode = this.developerMode.bind(this)
}
developerMode(){
this.props.history.push('/dev') //routes to developer component
}
render() {
return (
<div>
<List>
<ListItem
onClick={this.developerMode}>Developer<ListItem/>
</List>
</div>
);
}
}
开发者.js
class DeveloperMode extends Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {
url:'Prod'
};
}
handleChange(event) {
this.setState({
url: event.target.value
});
}
handleSubmit(event) {
event.preventDefault();
var env=this.state.url;
this.props.signOut();
}
render() {
return(
<div>
<h3>Select Environment </h3>
<RadioButtonGroup name="selectURL" defaultSelected="Prod" onChange={this.handleChange}>
<RadioButton
value="Prod"
label="Production"
/>
<RadioButton
value="Dev"
label="Development"
/>
<RadioButton
value="Test"
label="Testing"
/>
</RadioButtonGroup>
<RaisedButton label="Save" onClick={this.handleSubmit}/>
</div>
);
}
}
Routes.js
const Routes = () => (
<Router>
<div>
<Route component={LauncherApp}/>
<Switch>
<Route exact path={"/"} component={ApplicationList} />
<Route path={"/dev"} component={DeveloperMode} />
</Switch>
</div>
</Router>
);
当在 Launcher.js 中路由时,我有什么方法可以从开发者组件中获取值 'url'
【问题讨论】:
标签: reactjs react-router react-router-dom