@samitouri / QOS-React-2 / commits / 82f82c7543
Fork 0

The library for web and native user interfaces.

Cleanup "Working With the Browser"

Paul O’Shannessy committed Jul 16, 2013 at 23:30 UTC 82f82c7543ca8732165137d220230d588355d000
1 file changed +17 -4
docs/docs/07-working-with-the-browser.md
+17 -4
@@ -6,8 +6,10 @@ permalink: working-with-the-browser.html
6 prev: forms.html
7 next: more-about-refs.html
8 ---
9 +
10 React provides powerful abstractions that free you from touching the DOM directly in most cases, but sometimes you simply need to access the underlying API, perhaps to work with a third-party library or existing code.
11
12 +
13 ## The Mock DOM
14
15 React is so fast because it never talks to the DOM directly. React maintains a fast in-memory representation of the DOM. `render()` methods return a *description* of the DOM, and React can diff this description with the in-memory representation to compute the fastest way to update the browser.
@@ -16,11 +18,14 @@ Additionally, React implements a full synthetic event system such that all event
18
19 Most of the time you should stay within React's "faked browser" world since it's more performant and easier to reason about. However, sometimes you simply need to access the underlying API, perhaps to work with a third-party library like a jQuery plugin. React provides escape hatches for you to use the underlying DOM API directly.
20
21 +
22 ## Refs and getDOMNode()
23
24 To interact with the browser, you'll need a reference to a DOM node. Every mounted React component has a `getDOMNode()` function which you can call to get a reference to it.
25
23 -**Warning:** `getDOMNode()` only works on mounted components (that is, components that have been placed in the DOM). If you try to call this on a component that has not been mounted yet (like calling `getDOMNode()` in `render()` on a component that has yet to be created) an exception will be thrown.
26 +> Note:
27 +>
28 +> `getDOMNode()` only works on mounted components (that is, components that have been placed in the DOM). If you try to call this on a component that has not been mounted yet (like calling `getDOMNode()` in `render()` on a component that has yet to be created) an exception will be thrown.
29
30 In order to get a reference to a React component, you can either use `this` to get the current React component, or you can use refs to refer to a component you own. They work like this:
31
@@ -54,18 +59,22 @@ React.renderComponent(
59 );
60 ```
61
62 +
63 ## More About Refs
64
65 To learn more about refs, including ways to use them effectively, see our [more about refs](./more-about-refs.html) documentation.
66
67 +
68 ## Component Lifecycle
69
70 Components have three main parts of their lifecycle:
71 +
72 - **Mounting:** A component is being inserted into the DOM.
73 - **Updating:** A component is being re-rendered to determine if the DOM should be updated.
74 - **Unmounting:** A component is being removed from the DOM.
75
68 -React provides lifecycle methods that you can override to hook into this process. We provide **will** methods, which are called right before something happens, and **did** methods which are called right after something happens.
76 +React provides lifecycle methods that you can specify to hook into this process. We provide **will** methods, which are called right before something happens, and **did** methods which are called right after something happens.
77 +
78
79 ### Mounting
80
@@ -73,6 +82,7 @@ React provides lifecycle methods that you can override to hook into this process
82 - `componentWillMount()` is invoked immediately before mounting occurs.
83 - `componentDidMount(DOMElement rootNode)` is invoked immediately after mounting occurs. Initialization that requires DOM nodes should go here.
84
85 +
86 ### Updating
87
88 - `componentWillReceiveProps(object nextProps)` is invoked when a mounted component receives new props. This method should be used to compare `this.props` and `nextProps` to perform state transitions using `this.setState()`.
@@ -80,10 +90,12 @@ React provides lifecycle methods that you can override to hook into this process
90 - `componentWillUpdate(object nextProps, object nextState)` is invoked immediately before updating occurs. You cannot call `this.setState()` here.
91 - `componentDidUpdate(object prevProps, object prevState, DOMElement rootNode)` is invoked immediately after updating occurs.
92
93 +
94 ### Unmounting
95
96 - `componentWillUnmount()` is invoked immediately before a component is unmounted and destroyed. Cleanup should go here.
97
98 +
99 ### Mounted Methods
100
101 _Mounted_ composite components also support the following methods:
@@ -97,6 +109,7 @@ _Mounted_ composite components also support the following methods:
109 > `componentDidUpdate()` is a convenience. The same node can be obtained by
110 > calling `this.getDOMNode()`.
111
112 +
113 ## Browser Suppport and Polyfills
114
115 At Facebook, we support older browsers, including IE8. We've had polyfills in place for a long time to allow us to write forward-thinking JS. This means we don't have a bunch of hacks scattered throughout our codebase and we can still expect our code to "just work". For example, instead of seeing `+new Date()`, we can just write `Date.now()`. Since the open source React is the same as what we use internally, we've carried over this philosophy of using forward thinking JS.
@@ -115,5 +128,5 @@ In addition to that philosphy, we've also taken the stance that we, as authors o
128
129 All of these can be polyfilled using `es5-shim.js` from [https://github.com/kriskowal/es5-shim](https://github.com/kriskowal/es5-shim).
130
118 -* `console.*` - [https://github.com/paulmillr/console-polyfill](https://github.com/paulmillr/console-polyfill)
119 -* `Object.create` - Provided in `es5-sham.js` @ [https://github.com/kriskowal/es5-shim](https://github.com/kriskowal/es5-shim)
131 +* `console.*` - Only needed when not using the minified build. If you need to polyfill this, try [https://github.com/paulmillr/console-polyfill](https://github.com/paulmillr/console-polyfill).
132 +* `Object.create` - Provided in `es5-sham.js` @ [https://github.com/kriskowal/es5-shim](https://github.com/kriskowal/es5-shim).