--- id: faq-functions title: Passing Functions to Components permalink: docs/faq-functions.html layout: docs category: FAQ --- ### How do I pass an event handler (like onClick) to a component? Pass event handlers and other functions as props to child components: ```jsx ; } } ``` #### Class Properties (Stage 3 Proposal) ```jsx class Foo extends Component { // Note: this syntax is experimental and not standardized yet. handleClick = () => { console.log('Click happened'); } render() { return ; } } ``` #### Bind in Render ```jsx class Foo extends Component { handleClick() { console.log('Click happened'); } render() { return ; } } ``` >**Note:** > >Using `Function.prototype.bind` in render creates a new function each time the component renders, which may have performance implications (see below). #### Arrow Function in Render ```jsx class Foo extends Component { handleClick() { console.log('Click happened'); } render() { return ; } } ``` >**Note:** > >Using an arrow function in render creates a new function each time the component renders, which may have performance implications (see below). ### Is it OK to use arrow functions in render methods? Generally speaking, yes, it is OK, and it is often the easiest way to pass parameters to callback functions. If you do have performance issues, by all means, optimize! ### Why is binding necessary at all? In JavaScript, these two code snippets are **not** equivalent: ```js obj.method(); ``` ```js var method = obj.method; method(); ``` Binding methods helps ensure that the second snippet works the same way as the first one. With React, typically you only need to bind the methods you *pass* to other components. For example, ` } ``` Instead, *pass the function itself* (without parens): ```jsx render() { // Correct: handleClick is passed as a reference! return } ``` ### How do I pass a parameter to an event handler or callback? You can use an arrow function to wrap around an event handler and pass parameters: ```jsx ; } handleClick() { this.props.loadMore(); } } ``` #### Debounce Debouncing ensures that a function will not be executed until after a certain amount of time has passed since it was last called. This can be useful when you have to perform some expensive calculation in response to an event that might dispatch rapidly (eg scroll or keyboard events). The example below debounces text input with a 250ms delay. ```jsx import debounce from 'lodash.debounce'; class Searchbox extends React.Component { constructor(props) { super(props); this.handleChange = this.handleChange.bind(this); this.emitChangeDebounced = debounce(this.emitChange, 250); } componentWillUnmount() { this.emitChangeDebounced.cancel(); } render() { return ( ); } handleChange(e) { // React pools events, so we read the value before debounce. // Alternately we could call `event.persist()` and pass the entire event. // For more info see reactjs.org/docs/events.html#event-pooling this.emitChangeDebounced(e.target.value); } emitChange(value) { this.props.onChange(value); } } ```