Passing Functions to Components
How do I pass an event handler (like onClick) to a component?
Pass event handlers and other functions as props to child components:
If you need to have access to the parent component in the handler, you also need to bind the function to the component instance (see below).
How do I bind a function to a component instance?
There are several ways to make sure functions have access to component attributes like this.props
and this.state
, depending on which syntax and build steps you are using.
Bind in Constructor (ES2015)
Class Properties (Stage 3 Proposal)
Bind in Render
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
Note:
Using an arrow function in render creates a new function each time the component renders, which may break optimizations based on strict identity comparison.
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:
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, <button onClick={this.handleClick}>
passes this.handleClick
so you want to bind it. However, it is unnecessary to bind the render
method or the lifecycle methods: we don't pass them to other components.
Why is my function being called every time the component renders?
Make sure you aren't calling the function when you pass it to the component:
Instead, pass the function itself (without parens):
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:
This is equivalent to calling .bind
:
Example: Passing params using arrow functions
Example: Passing params using data-attributes
Alternately, you can use DOM APIs to store data needed for event handlers. Consider this approach if you need to optimize a large number of elements or have a render tree that relies on React.PureComponent equality checks.
How can I prevent a function from being called too quickly or too many times in a row?
If you have an event handler such as onClick
or onScroll
and want to prevent the callback from being fired too quickly, then you can limit the rate at which callback is executed. This can be done by using:
Note:
_.debounce
,_.throttle
andraf-schd
provide acancel
method to cancel delayed callbacks. You should either call this method fromcomponentWillUnmount
or check to ensure that the component is still mounted within the delayed function.
Throttle
Throttling prevents a function from being called more than once in a given window of time. The example below throttles a "click" handler to prevent calling it more than once per second.
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.
requestAnimationFrame
throttling
Note:
Testing your rate limiting
Last updated
Was this helpful?