简体   繁体   English

如何在React中将事件处理程序传递给子组件

[英]How to pass an event handler to a child component in React

I have a <Button /> component I've created in React that abstracts out some of the styling in my application. 我有一个我在React中创建的<Button />组件,它在我的应用程序中抽象出一些样式。 I am using it in two different contexts - one to submit a login form, and the other to navigate to the registration page (and probably other contexts in the future). 我在两个不同的上下文中使用它 - 一个用于提交登录表单,另一个用于导航到注册页面(以及将来可能的其他上下文)。

I am trying to figure out how to pass the event handlers from the parent component to the <Button /> . 我试图弄清楚如何将事件处理程序从父组件传递到<Button /> I want to call an onSubmit handler for the login form, but an onClick handler for the navigation button. 我想为登录表单调用onSubmit处理程序,但是为导航按钮调用onClick处理程序。 Is this possible? 这可能吗?

I have tried calling the component like this: 我试过像这样调用组件:

<Button text={callToAction} style={styles.callToActionButton} onClick={() => FlowRouter.go("Auth")}/>

<Button text="Go!" style={styles.registerButton} onSubmit={() => this.register(this.state.user, this.state.password)}/>

I've also tried removing the arrow function, which just causes the functions to execute when the component is loaded: 我也尝试删除箭头函数,这只会导致函数在加载组件时执行:

// executes event handlers on page load
<Button text={callToAction} style={styles.callToActionButton} onClick={FlowRouter.go("Auth")}/>

<Button text="Go!" style={styles.registerButton} onSubmit={this.register(this.state.user, this.state.password)}/>

In general, you can forward the onClick handler to your button class by passing it as a property. 通常,您可以将onClick处理程序作为属性传递给您的按钮类。 You could this make a required prop by simply defining the propTypes for your button component. 您可以通过简单地定义按钮组件的propTypes来创建所需的prop。

As an example, I added a small snippet that shows how it works 作为一个例子,我添加了一个小片段,显示它是如何工作的

 var StyledButton = React.createClass({ propTypes: { // the StyledButton requires a clickHandler clickHandler: React.PropTypes.func.Required, // and I guess the text can be seen as required as well text: React.PropTypes.string.required }, render: function() { // as you are sure you have a clickHandler, you can just reference it directly from the props return <button type="button" onClick={this.props.clickHandler}>{this.props.text}</button>; } }); var MyForm = React.createClass({ getInitialState() { return { clicked: 0 }; }, click() { this.setState({clicked: this.state.clicked+1}); alert('ouch'); }, secondClickHandler() { this.setState({clicked: 0}); alert(':('); }, render() { // your parent component simply sets which button return <fieldset> <div> <StyledButton clickHandler={this.click} text="Click me" /> { (this.state.clicked > 0) && <StyledButton clickHandler={this.secondClickHandler} text="Not again" /> } </div> </fieldset>; } }); ReactDOM.render( <MyForm />, document.getElementById('container') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.2/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.2/react-dom.min.js"></script> <div id="container"> <!-- This element's contents will be replaced with your component. --> </div> 

Also, you wouldn't in general use the submit method of a button, you would rather send the data received to a webservice, and handle any changes when the result is received. 此外,您通常不会使用按钮的提交方法,而是希望将收到的数据发送到Web服务,并在收到结果时处理任何更改。 The submit kills the current website and needs to load everything anew, while with an ajax call, or a store, it can just wait for the result and then redirect the user based on the response 提交会杀死当前网站并需要重新加载所有内容,而使用ajax调用或商店时,它可以等待结果,然后根据响应重定向用户

How we have handled this is we have a button component that renders an a tag and then we have a href prop and a onClick prop you can pass in. If its a link just pass in the href prop to the button and if you are wanting it to execute a function just pass it in an onClick prop and make sure it gets set on the a tag. 我们如何处理这个问题是我们有一个按钮组件呈现一个标签,然后我们有一个href prop和一个你可以传入的onClick prop。如果它的链接只是将href prop传递给按钮,如果你想要的话它执行一个函数只是将它传递给onClick prop并确保它在a标签上设置。

In the Button component we also setup a custom onClick function that looks like this: 在Button组件中,我们还设置了一个自定义onClick函数,如下所示:

_onClick: function(e) {
  if (!this.props.onClick) {
    return; 
  }
  this.props.onClick(e);
}

and then on the a tag 然后在标签上

<a href={this.props.href} onClick={this._onClick} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM