Passing Functions to Components

  • 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).
  • There are several ways to make sure functions have access to component attributes like and , depending on which syntax and build steps you are using.
  • 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.
  • Make sure you aren’t calling the function when you pass it to the component: – – Instead, pass the function itself (without parens): – – You can use an arrow function to wrap around an event handler and pass parameters: – – This is equivalent to calling : – -…
  • is a way of queuing a function to be executed in the browser at the optimal time for rendering performance.

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).
Continue reading “Passing Functions to Components”