Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions ghdocs/BESTPRACTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,26 @@ public render() {
```

Best, use createRef:

The `createRef` function in `lib/Utilities` is a polyfill for [React.createRef](https://github.com/facebook/react/pull/11555). (When Fabric switches over to React 16, we'll use React.createRef instead)

`createRef` creates a reference object that has the following type `{ value: T | null }`, where T is the element to reference (either a dom node or a react component). You set the reference by passing the reference object as the `ref` prop. You can then subsequently access the reference through the `.value` property on the reference object elsewhere in your code.

```typescript
import { createRef } from 'office-ui-fabric-react/lib/Utilities';

class Foo extends BaseComponent<...> {
private _root = createRef<HTMLElement>();
// Create the reference object that will be used for setting and accessing the reference
private _root = createRef<HTMLButtonElement>();

public render() {
return <div ref={ _root } />;
// Set the reference by passing the reference object as the ref prop
return <button ref={ _root } onClick={this._onClick} />;
}

private _onClick() {
// Access the reference through the .value property on the reference object
this._root.value.focus();
}
}
```
Expand Down