Extending with callbacks
Another method to create more pluggable components is to expose (and act on) event callbacks. We saw something similar already, but let's take a look at this anyway. Suppose we have a PageEditorComponent
class, as follows:
import React from "react"; class PageEditorComponent extends React.Component { onSave(e, refs) { this.props.onSave(e, refs); } onCancel(e, refs) { this.props.onCancel(e, refs); } render() { let refs = {}; return ( <div> <input type="text" ref={ref => refs.title = ref} /> <input type="text" ref={ref => refs.body = ref} /> <button onClick={e => this.onSave(e, refs)}> save </button> <button onClick={e => this.onCancel(e, refs)}> cancel </button> ...