|
| 1 | +--- |
| 2 | +id: faq-functions |
| 3 | +title: Passing Functions to Components |
| 4 | +permalink: docs/faq-functions.html |
| 5 | +layout: docs |
| 6 | +category: FAQ |
| 7 | +--- |
| 8 | + |
| 9 | +### How do I pass an event handler (like onClick) to a component? |
| 10 | + |
| 11 | +Pass event handlers and other functions as props to child components: |
| 12 | + |
| 13 | +```jsx |
| 14 | +<button onClick={this.handleClick}> |
| 15 | +``` |
| 16 | + |
| 17 | +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). |
| 18 | + |
| 19 | +### How do I bind a function to a component instance? |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +#### Bind in Constructor (ES2015) |
| 24 | + |
| 25 | +```jsx |
| 26 | +class Foo extends Component { |
| 27 | + constructor () { |
| 28 | + this.handleClick = this.handleClick.bind(this) |
| 29 | + } |
| 30 | + handleClick() { |
| 31 | + console.log('Click happened') |
| 32 | + } |
| 33 | + render() { |
| 34 | + return <button onClick={this.handleClick}>Click Me</button> |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +#### Class Properties (Stage 3 Proposal) |
| 40 | + |
| 41 | +```jsx |
| 42 | +class Foo extends Component { |
| 43 | + handleClick = () => { |
| 44 | + console.log('Click happened') |
| 45 | + } |
| 46 | + render() { |
| 47 | + return <button onClick={this.handleClick}>Click Me</button> |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +#### Bind in Render |
| 53 | + |
| 54 | +```jsx |
| 55 | +class Foo extends Component { |
| 56 | + handleClick () { |
| 57 | + console.log('Click happened') |
| 58 | + } |
| 59 | + render() { |
| 60 | + return <button onClick={this.handleClick.bind(this)}>Click Me</button> |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +>**Note:** |
| 66 | +> |
| 67 | +>Using `Function.prototype.bind` in render creates a new function each time the component renders, which may have performance implications; (see below). |
| 68 | +
|
| 69 | +#### Arrow Function in Render |
| 70 | + |
| 71 | +```jsx |
| 72 | +class Foo extends Component { |
| 73 | + handleClick () { |
| 74 | + console.log('Click happened') |
| 75 | + } |
| 76 | + render() { |
| 77 | + return <button onClick={() => this.handleClick()}>Click Me</button> |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +>**Note:** |
| 83 | +> |
| 84 | +>Using an arrow function in render creates a new function each time the component renders, which may have performance implications; (see below). |
| 85 | +
|
| 86 | +### Is it OK to use arrow functions in render methods? |
| 87 | + |
| 88 | +Generally speaking, yes, it is OK, and it is often the easiest way to pass parameters to callback functions. |
| 89 | + |
| 90 | +If you do have performance issues, by all means, optimize! |
| 91 | + |
| 92 | +### Why is my function being called every time the component renders? |
| 93 | + |
| 94 | +Make sure you aren't _calling the function_ when you pass it to the component: |
| 95 | + |
| 96 | +```jsx |
| 97 | +render() { |
| 98 | + {/* handleClick is called instead of passed as a reference! */} |
| 99 | + return <button onClick={this.handleClick()}>Click Me</button> |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### How do I pass a parameter to an event handler or callback? |
| 104 | + |
| 105 | +You can use an arrow function to wrap around an event handler and pass parameters: |
| 106 | + |
| 107 | +```jsx |
| 108 | +<Element onClick={() => this.handleClick(id)} /> |
| 109 | +``` |
| 110 | + |
| 111 | +This is equivalent to calling `.bind`: |
| 112 | + |
| 113 | +```jsx |
| 114 | +<Element onClick={this.handleClick.bind(this, id)} /> |
| 115 | +``` |
| 116 | + |
| 117 | +#### Example: Passing params using arrow functions |
| 118 | + |
| 119 | +```jsx |
| 120 | +const A = 65 // ASCII character code |
| 121 | +class Alphabet extends React.Component { |
| 122 | + state = { |
| 123 | + justClicked: null, |
| 124 | + letters: Array.from({length: 26}, (_, i) => String.fromCharCode(A + i)) |
| 125 | + } |
| 126 | + |
| 127 | + handleClick = letter => this.setState({ justClicked: letter }) |
| 128 | + |
| 129 | + render () { |
| 130 | + return ( |
| 131 | + <div> |
| 132 | + Just clicked: {this.state.justClicked} |
| 133 | + <ul> |
| 134 | + { this.state.letters.map(letter => |
| 135 | + <li key={letter} onClick={() => this.handleClick(letter)}> |
| 136 | + {letter} |
| 137 | + </li> |
| 138 | + ) } |
| 139 | + </ul> |
| 140 | + </div> |
| 141 | + ) |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +#### Example: Passing params using data-attributes |
| 147 | + |
| 148 | +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. |
| 149 | + |
| 150 | +```jsx |
| 151 | +const A = 65 // ASCII character code |
| 152 | +class Alphabet extends React.Component { |
| 153 | + state = { |
| 154 | + justClicked: null, |
| 155 | + letters: Array.from({length: 26}, (_, i) => String.fromCharCode(A + i)) |
| 156 | + } |
| 157 | + |
| 158 | + handleClick = event => { |
| 159 | + this.setState({ |
| 160 | + justClicked: event.target.dataset.letter |
| 161 | + }) |
| 162 | + } |
| 163 | + |
| 164 | + render () { |
| 165 | + return ( |
| 166 | + <div> |
| 167 | + Just clicked: {this.state.justClicked} |
| 168 | + <ul> |
| 169 | + { this.state.letters.map(letter => |
| 170 | + <li key={letter} data-letter={letter} onClick={this.handleClick}> |
| 171 | + {letter} |
| 172 | + </li> |
| 173 | + ) } |
| 174 | + </ul> |
| 175 | + </div> |
| 176 | + ) |
| 177 | + } |
| 178 | +} |
| 179 | +``` |
0 commit comments