You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
7.3 KiB
149 lines
7.3 KiB
12 years ago
|
---
|
||
12 years ago
|
id: working-with-the-browser
|
||
12 years ago
|
title: Working With the Browser
|
||
12 years ago
|
permalink: working-with-the-browser.html
|
||
|
prev: forms.html
|
||
|
next: more-about-refs.html
|
||
12 years ago
|
---
|
||
12 years ago
|
|
||
12 years ago
|
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.
|
||
12 years ago
|
|
||
12 years ago
|
|
||
11 years ago
|
## The Virtual DOM
|
||
12 years ago
|
|
||
|
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.
|
||
|
|
||
|
Additionally, React implements a full synthetic event system such that all event objects are guaranteed to conform to the W3C spec despite browser quirks, and everything bubbles consistently and in a performant way cross-browser. You can even use some HTML5 events in IE8!
|
||
|
|
||
|
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.
|
||
|
|
||
12 years ago
|
|
||
12 years ago
|
## Refs and getDOMNode()
|
||
12 years ago
|
|
||
|
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.
|
||
|
|
||
12 years ago
|
> Note:
|
||
|
>
|
||
|
> `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.
|
||
12 years ago
|
|
||
|
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:
|
||
|
|
||
|
```javascript
|
||
|
var MyComponent = React.createClass({
|
||
|
handleClick: function() {
|
||
|
// Explicitly focus the text input using the raw DOM API.
|
||
|
this.refs.myTextInput.getDOMNode().focus();
|
||
|
},
|
||
|
render: function() {
|
||
|
// The ref attribute adds a reference to the component to
|
||
|
// this.refs when the component is mounted.
|
||
|
return (
|
||
|
<div>
|
||
|
<input type="text" ref="myTextInput" />
|
||
|
<input
|
||
|
type="button"
|
||
|
value="Focus the text input"
|
||
|
onClick={this.handleClick}
|
||
|
/>
|
||
|
</div>
|
||
|
);
|
||
|
}
|
||
|
});
|
||
|
|
||
10 years ago
|
React.render(
|
||
12 years ago
|
<MyComponent />,
|
||
|
document.getElementById('example')
|
||
|
);
|
||
|
```
|
||
|
|
||
12 years ago
|
|
||
12 years ago
|
## More About Refs
|
||
12 years ago
|
|
||
11 years ago
|
To learn more about refs, including ways to use them effectively, see our [more about refs](/react/docs/more-about-refs.html) documentation.
|
||
12 years ago
|
|
||
12 years ago
|
|
||
12 years ago
|
## Component Lifecycle
|
||
12 years ago
|
|
||
|
Components have three main parts of their lifecycle:
|
||
12 years ago
|
|
||
12 years ago
|
* **Mounting:** A component is being inserted into the DOM.
|
||
|
* **Updating:** A component is being re-rendered to determine if the DOM should be updated.
|
||
|
* **Unmounting:** A component is being removed from the DOM.
|
||
12 years ago
|
|
||
12 years ago
|
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.
|
||
|
|
||
12 years ago
|
|
||
12 years ago
|
### Mounting
|
||
12 years ago
|
|
||
12 years ago
|
* `getInitialState(): object` is invoked before a component is mounted. Stateful components should implement this and return the initial state data.
|
||
|
* `componentWillMount()` is invoked immediately before mounting occurs.
|
||
11 years ago
|
* `componentDidMount()` is invoked immediately after mounting occurs. Initialization that requires DOM nodes should go here.
|
||
12 years ago
|
|
||
12 years ago
|
|
||
12 years ago
|
### Updating
|
||
12 years ago
|
|
||
12 years ago
|
* `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()`.
|
||
|
* `shouldComponentUpdate(object nextProps, object nextState): boolean` is invoked when a component decides whether any changes warrant an update to the DOM. Implement this as an optimization to compare `this.props` with `nextProps` and `this.state` with `nextState` and return false if React should skip updating.
|
||
|
* `componentWillUpdate(object nextProps, object nextState)` is invoked immediately before updating occurs. You cannot call `this.setState()` here.
|
||
11 years ago
|
* `componentDidUpdate(object prevProps, object prevState)` is invoked immediately after updating occurs.
|
||
12 years ago
|
|
||
12 years ago
|
|
||
12 years ago
|
### Unmounting
|
||
12 years ago
|
|
||
12 years ago
|
* `componentWillUnmount()` is invoked immediately before a component is unmounted and destroyed. Cleanup should go here.
|
||
12 years ago
|
|
||
12 years ago
|
|
||
12 years ago
|
### Mounted Methods
|
||
12 years ago
|
|
||
|
_Mounted_ composite components also support the following methods:
|
||
|
|
||
12 years ago
|
* `getDOMNode(): DOMElement` can be invoked on any mounted component in order to obtain a reference to its rendered DOM node.
|
||
|
* `forceUpdate()` can be invoked on any mounted component when you know that some deeper aspect of the component's state has changed without using `this.setState()`.
|
||
12 years ago
|
|
||
12 years ago
|
|
||
12 years ago
|
## Browser Support and Polyfills
|
||
12 years ago
|
|
||
|
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.
|
||
|
|
||
12 years ago
|
In addition to that philosophy, we've also taken the stance that we, as authors of a JS library, should not be shipping polyfills as a part of our library. If every library did this, there's a good chance you'd be sending down the same polyfill multiple times, which could be a sizable chunk of dead code. If your product needs to support older browsers, chances are you're already using something like [es5-shim](https://github.com/kriskowal/es5-shim).
|
||
12 years ago
|
|
||
|
|
||
12 years ago
|
### Polyfills Needed to Support Older Browsers
|
||
12 years ago
|
|
||
11 years ago
|
`es5-shim.js` from [kriskowal's es5-shim](https://github.com/kriskowal/es5-shim) provides the following that React needs:
|
||
11 years ago
|
|
||
12 years ago
|
* `Array.isArray`
|
||
11 years ago
|
* `Array.prototype.every`
|
||
12 years ago
|
* `Array.prototype.forEach`
|
||
|
* `Array.prototype.indexOf`
|
||
11 years ago
|
* `Array.prototype.map`
|
||
12 years ago
|
* `Date.now`
|
||
11 years ago
|
* `Function.prototype.bind`
|
||
11 years ago
|
* `Object.keys`
|
||
11 years ago
|
* `String.prototype.split`
|
||
|
* `String.prototype.trim`
|
||
12 years ago
|
|
||
11 years ago
|
`es5-sham.js`, also from [kriskowal's es5-shim](https://github.com/kriskowal/es5-shim), provides the following that React needs:
|
||
12 years ago
|
|
||
11 years ago
|
* `Object.create`
|
||
11 years ago
|
* `Object.freeze`
|
||
11 years ago
|
|
||
|
The unminified build of React needs the following from [paulmillr's console-polyfill](https://github.com/paulmillr/console-polyfill).
|
||
|
|
||
|
* `console.*`
|
||
11 years ago
|
|
||
11 years ago
|
When using HTML5 elements in IE8 including `<section>`, `<article>`, `<nav>`, `<header>`, and `<footer>`, it's also necessary to include [html5shiv](https://github.com/aFarkas/html5shiv) or a similar script.
|
||
|
|
||
11 years ago
|
|
||
|
### Cross-browser Issues
|
||
|
|
||
11 years ago
|
Although React is pretty good at abstracting browser differences, some browsers are limited or present quirky behaviors that we couldn't find a workaround for.
|
||
11 years ago
|
|
||
|
|
||
|
#### onScroll event on IE8
|
||
|
|
||
11 years ago
|
On IE8 the `onScroll` event doesn't bubble and IE8 doesn't have an API to define handlers to the capturing phase of an event, meaning there is no way for React to listen to these events.
|
||
11 years ago
|
Currently a handler to this event is ignored on IE8.
|
||
11 years ago
|
|
||
|
See the [onScroll doesn't work in IE8](https://github.com/facebook/react/issues/631) GitHub issue for more information.
|