From 0b5431b795700b02fcbede7dcc9c7106ff040107 Mon Sep 17 00:00:00 2001 From: Lyubo Marinov Date: Wed, 19 Jul 2017 16:26:27 -0500 Subject: [PATCH] [RN] Bind event handler once per instance, not per render --- .../components/AbstractToolbarButton.js | 27 +++++++++++++++++++ .../components/ToolbarButton.native.js | 4 +-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/react/features/toolbox/components/AbstractToolbarButton.js b/react/features/toolbox/components/AbstractToolbarButton.js index d8f37f0fd..d8fadbc48 100644 --- a/react/features/toolbox/components/AbstractToolbarButton.js +++ b/react/features/toolbox/components/AbstractToolbarButton.js @@ -42,6 +42,33 @@ export default class AbstractToolbarButton extends Component { underlayColor: React.PropTypes.any }; + /** + * Initializes a new {@code AbstractToolbarButton} instance. + * + * @param {Object} props - The React {@code Component} props to initialize + * the new {@code AbstractToolbarButton} instance with. + */ + constructor(props) { + super(props); + + // Bind event handlers so they are only bound once per instance. + this._onClick = this._onClick.bind(this); + } + + /** + * Handles clicking/pressing this {@code AbstractToolbarButton} by + * forwarding the event to the {@code onClick} prop of this instance if any. + * + * @protected + * @returns {*} The result returned by the invocation of the {@code onClick} + * prop of this instance if any. + */ + _onClick(...args) { + const { onClick } = this.props; + + return onClick && onClick(...args); + } + /** * Implements React's {@link Component#render()}. * diff --git a/react/features/toolbox/components/ToolbarButton.native.js b/react/features/toolbox/components/ToolbarButton.native.js index 5c503decc..e19dd1b14 100644 --- a/react/features/toolbox/components/ToolbarButton.native.js +++ b/react/features/toolbox/components/ToolbarButton.native.js @@ -38,9 +38,7 @@ class ToolbarButton extends AbstractToolbarButton { const props = {}; 'disabled' in this.props && (props.disabled = this.props.disabled); - 'onClick' in this.props && (props.onPress = event => { - this.props.onClick(event); - }); + 'onClick' in this.props && (props.onPress = this._onClick); 'style' in this.props && (props.style = this.props.style); 'underlayColor' in this.props && (props.underlayColor = this.props.underlayColor);