Event handler context and parameters
In this section, we'll examine React components that automatically bind their event handler contexts and how you can pass data into event handlers. Having the right context is important for React event handler functions, because they usually need access to properties or state of the component. Being able to parameterize event handlers is also important, because they don't pull data out of DOM elements.
Auto-binding context
The components you've implemented so far in this book have used the ES2015 class style declaration. This is where you declare a class that extends the base React Component
class. When you do this, however, any event handler methods in the component will need to be manually bound to the component context. For example, if you need access to this.props
, this
needs to be a reference to the component.
You can use the React.createClass()
function to declare a component and have its method contexts auto bind to the component. In other words,...