ソースを参照

[RN] Bind event handler once per instance, not per render

j8
Lyubo Marinov 8年前
コミット
0b5431b795

+ 27
- 0
react/features/toolbox/components/AbstractToolbarButton.js ファイルの表示

@@ -42,6 +42,33 @@ export default class AbstractToolbarButton extends Component {
42 42
         underlayColor: React.PropTypes.any
43 43
     };
44 44
 
45
+    /**
46
+     * Initializes a new {@code AbstractToolbarButton} instance.
47
+     *
48
+     * @param {Object} props - The React {@code Component} props to initialize
49
+     * the new {@code AbstractToolbarButton} instance with.
50
+     */
51
+    constructor(props) {
52
+        super(props);
53
+
54
+        // Bind event handlers so they are only bound once per instance.
55
+        this._onClick = this._onClick.bind(this);
56
+    }
57
+
58
+    /**
59
+     * Handles clicking/pressing this {@code AbstractToolbarButton} by
60
+     * forwarding the event to the {@code onClick} prop of this instance if any.
61
+     *
62
+     * @protected
63
+     * @returns {*} The result returned by the invocation of the {@code onClick}
64
+     * prop of this instance if any.
65
+     */
66
+    _onClick(...args) {
67
+        const { onClick } = this.props;
68
+
69
+        return onClick && onClick(...args);
70
+    }
71
+
45 72
     /**
46 73
      * Implements React's {@link Component#render()}.
47 74
      *

+ 1
- 3
react/features/toolbox/components/ToolbarButton.native.js ファイルの表示

@@ -38,9 +38,7 @@ class ToolbarButton extends AbstractToolbarButton {
38 38
         const props = {};
39 39
 
40 40
         'disabled' in this.props && (props.disabled = this.props.disabled);
41
-        'onClick' in this.props && (props.onPress = event => {
42
-            this.props.onClick(event);
43
-        });
41
+        'onClick' in this.props && (props.onPress = this._onClick);
44 42
         'style' in this.props && (props.style = this.props.style);
45 43
         'underlayColor' in this.props
46 44
             && (props.underlayColor = this.props.underlayColor);

読み込み中…
キャンセル
保存