diff --git a/apps/fabric-website/src/components/App/App.tsx b/apps/fabric-website/src/components/App/App.tsx index e8f2ddcc338765..38b5c98c5e03ad 100644 --- a/apps/fabric-website/src/components/App/App.tsx +++ b/apps/fabric-website/src/components/App/App.tsx @@ -1,7 +1,6 @@ import * as React from 'react'; import './App.scss'; import { AppState } from './AppState'; -import { autobind } from 'office-ui-fabric-react/lib/Utilities'; import AttachedScrollUtility from '../../utilities/AttachedScrollUtility'; import { Fabric } from 'office-ui-fabric-react/lib/Fabric'; import { Nav } from '../Nav/Nav'; @@ -75,8 +74,7 @@ export class App extends React.Component { ); } - @autobind - private _handleNavPositioning() { + private _handleNavPositioning = () => { let { isAttached, navHeight } = this.state; this._appContentRect = this._appContent && this._appContent.getBoundingClientRect(); const viewPortHeight = window.innerHeight; diff --git a/apps/todo-app/src/components/TodoForm.tsx b/apps/todo-app/src/components/TodoForm.tsx index f2c71d9439c075..c381a5fd8980a8 100644 --- a/apps/todo-app/src/components/TodoForm.tsx +++ b/apps/todo-app/src/components/TodoForm.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { autobind, BaseComponent, IBaseProps, createRef } from 'office-ui-fabric-react/lib/Utilities'; +import { BaseComponent, IBaseProps, createRef } from 'office-ui-fabric-react/lib/Utilities'; import { PrimaryButton } from 'office-ui-fabric-react/lib/Button'; import { TextField, ITextField } from 'office-ui-fabric-react/lib/TextField'; import * as stylesImport from './Todo.scss'; @@ -78,8 +78,7 @@ export default class TodoForm extends BaseComponent): void { + private _onSubmit = (event: React.FormEvent): void => { event.preventDefault(); const { value: textField } = this._textField; diff --git a/apps/todo-app/src/components/TodoTabs.tsx b/apps/todo-app/src/components/TodoTabs.tsx index 1905304763535a..62ba76a273e82a 100644 --- a/apps/todo-app/src/components/TodoTabs.tsx +++ b/apps/todo-app/src/components/TodoTabs.tsx @@ -7,7 +7,7 @@ import { } from 'office-ui-fabric-react/lib/Pivot'; import { FocusZone, FocusZoneDirection } from 'office-ui-fabric-react/lib/FocusZone'; import { List } from 'office-ui-fabric-react/lib/List'; -import { KeyCodes, autobind } from 'office-ui-fabric-react/lib/Utilities'; +import { KeyCodes } from 'office-ui-fabric-react/lib/Utilities'; import TodoItem from './TodoItem'; import { ITodoItem, ITodoItemProps, ITodoTabsProps } from '../types/index'; @@ -73,8 +73,7 @@ export default class TodoTabs extends React.Component { ); } - @autobind - private _isInnerZoneKeystroke(ev: React.KeyboardEvent): boolean { + private _isInnerZoneKeystroke = (ev: React.KeyboardEvent): boolean => { return ev.which === KeyCodes.right; } diff --git a/common/changes/@uifabric/example-app-base/retire-autobind_2018-03-17-15-50.json b/common/changes/@uifabric/example-app-base/retire-autobind_2018-03-17-15-50.json new file mode 100644 index 00000000000000..16c3ecf1205f38 --- /dev/null +++ b/common/changes/@uifabric/example-app-base/retire-autobind_2018-03-17-15-50.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "@uifabric/example-app-base", + "comment": "Use arrow function properties instead of @autobind", + "type": "patch" + } + ], + "packageName": "@uifabric/example-app-base", + "email": "mark@thedutchies.com" +} \ No newline at end of file diff --git a/common/changes/@uifabric/experiments/retire-autobind_2018-03-17-15-50.json b/common/changes/@uifabric/experiments/retire-autobind_2018-03-17-15-50.json new file mode 100644 index 00000000000000..8913998ae14afa --- /dev/null +++ b/common/changes/@uifabric/experiments/retire-autobind_2018-03-17-15-50.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "@uifabric/experiments", + "comment": "Use arrow function properties instead of @autobind", + "type": "patch" + } + ], + "packageName": "@uifabric/experiments", + "email": "mark@thedutchies.com" +} \ No newline at end of file diff --git a/common/changes/@uifabric/fabric-website/retire-autobind_2018-03-17-15-50.json b/common/changes/@uifabric/fabric-website/retire-autobind_2018-03-17-15-50.json new file mode 100644 index 00000000000000..ee8ddbad800393 --- /dev/null +++ b/common/changes/@uifabric/fabric-website/retire-autobind_2018-03-17-15-50.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "@uifabric/fabric-website", + "comment": "Use arrow function properties instead of @autobind", + "type": "patch" + } + ], + "packageName": "@uifabric/fabric-website", + "email": "mark@thedutchies.com" +} \ No newline at end of file diff --git a/common/changes/@uifabric/utilities/retire-autobind_2018-03-17-15-50.json b/common/changes/@uifabric/utilities/retire-autobind_2018-03-17-15-50.json new file mode 100644 index 00000000000000..6ee10a304e0947 --- /dev/null +++ b/common/changes/@uifabric/utilities/retire-autobind_2018-03-17-15-50.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "@uifabric/utilities", + "comment": "Use arrow function properties instead of @autobind", + "type": "patch" + } + ], + "packageName": "@uifabric/utilities", + "email": "mark@thedutchies.com" +} \ No newline at end of file diff --git a/common/changes/office-ui-fabric-react/retire-autobind_2018-03-17-15-50.json b/common/changes/office-ui-fabric-react/retire-autobind_2018-03-17-15-50.json new file mode 100644 index 00000000000000..071c5ddd6dee24 --- /dev/null +++ b/common/changes/office-ui-fabric-react/retire-autobind_2018-03-17-15-50.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "office-ui-fabric-react", + "comment": "Use arrow function properties instead of @autobind", + "type": "patch" + } + ], + "packageName": "office-ui-fabric-react", + "email": "mark@thedutchies.com" +} \ No newline at end of file diff --git a/common/changes/todo-app/retire-autobind_2018-03-17-15-50.json b/common/changes/todo-app/retire-autobind_2018-03-17-15-50.json new file mode 100644 index 00000000000000..e00f2578a18d72 --- /dev/null +++ b/common/changes/todo-app/retire-autobind_2018-03-17-15-50.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "todo-app", + "comment": "Use arrow function properties instead of @autobind", + "type": "patch" + } + ], + "packageName": "todo-app", + "email": "mark@thedutchies.com" +} \ No newline at end of file diff --git a/ghdocs/BESTPRACTICES.md b/ghdocs/BESTPRACTICES.md index 9b8481f5b30cc3..5991e007a5e69c 100644 --- a/ghdocs/BESTPRACTICES.md +++ b/ghdocs/BESTPRACTICES.md @@ -123,21 +123,38 @@ BAD: GOOD: `onRenderItem: IRenderFunction` -## Use @autobind. Avoid ALL binds in templates +## Use arrow function properties to avoid ALL binds in templates -The autobind decorator simplifies making event callbacks "bound" to the instance. It's easy to use: +When we use bind in a template, it means that the function is recreated every time, which is an anti-pattern. +BAD: ```typescript class Foo { - @autobind public _onClick(ev: React.MouseEvent) { } + render() { + { + } + + render() { + { // Set the reference by passing the reference object as the ref prop return