【发布时间】:2017-03-23 16:21:39
【问题描述】:
我是 React 的新手,因此是这个问题。 我有一个父组件 - HomePage 和一个子组件 - SideBar。
我的子组件侧边栏需要在提交按钮单击时将数据传递回父级,父级需要在 api 上发布。
这是我的父组件,
class HomePage extends React.Component{
constructor(props) {
.......
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(){
//Logic to get the data from the child and post to localhost:8080/
}
render(){
return(
<div className="col-md-2 left-pane">
<Sidebar handleSubmitButton={this.state.handleSubmit}/>
</div>
);
}
}
这是我的子组件,
class Sidebar extends React.Component {
handleSubmitButton(event) {
event.preventDefault();
}
render() {
return (
<div>
<input type="text"/>
<button type="button" className="btn btn-info btn-icons" onClick={this.props.handleSubmitButton} >
<span className="glyphicon glyphicon-send" aria-hidden="true"/>
</button>
</div>
);
}
}
Sidebar.propTypes = {
handleSubmitButton: React.PropTypes.func
};
我的问题是如何使用侧边栏按钮单击上的 onclick 方法获取输入文本并将其传递给父级以将其发布到 api 上。任何帮助表示赞赏。
【问题讨论】:
标签: javascript reactjs dom