Files
react/docs/advanced-components.html
T
2013-06-05 09:08:04 -07:00

230 lines
12 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>React | Advanced Components</title>
<meta name="viewport" content="width=device-width">
<meta property="og:title" content="React | Advanced Components" />
<meta property="og:type" content="website" />
<meta property="og:url" content="http://facebook.github.io/react/docs/advanced-components.html" />
<meta property="og:image" content="http://facebook.github.io/react/img/logo_og.png" />
<meta property="og:description" content="A JavaScript library for building user interfaces" />
<link rel="shortcut icon" href="/react/favicon.ico">
<link rel="alternate" type="application/rss+xml" title="React" href="http://facebook.github.io/react/feed.xml">
<link rel="stylesheet" href="/react/css/react.css">
<link rel="stylesheet" href="/react/css/syntax.css">
<link rel="stylesheet" href="/react/css/codemirror.css">
<script type="text/javascript" src="//use.typekit.net/vqa1hcx.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
<script type="text/javascript" src="/react/js/codemirror.js"></script>
<script type="text/javascript" src="/react/js/javascript.js"></script>
<script type="text/javascript" src="/react/js/react.min.js"></script>
<script type="text/javascript" src="/react/js/JSXTransformer.js"></script>
<script type="text/javascript" src="/react/js/live_editor.js"></script>
<script type="text/javascript" src="/react/js/showdown.js"></script>
</head>
<body>
<div class="container">
<div class="nav-main">
<div class="wrap">
<a class="nav-home" href="/react/index.html">
<img class="nav-logo" alt="" src="/react/img/logo_small.png">
React
</a>
<ul class="nav-site">
<li><a href="/react/docs/getting-started.html" class="active">docs</a></li>
<li><a href="/react/support.html">support</a></li>
<li><a href="/react/downloads.html">download</a></li>
<li><a href="/react/blog/">blog</a></li>
<li><a href="http://github.com/facebook/react">github</a>
</ul>
<!-- <iframe src="http://ghbtns.com/github&#45;btn.html?user=facebook&#38;repo=react.js&#38;type=fork"allowtransparency="true" frameborder="0" scrolling="0" width="62" height="20"></iframe> -->
</div>
</div>
<section class="content wrap documentationContent">
<div class="nav-docs">
<div class="nav-docs-section">
<h3>Getting started</h3>
<ul>
<li><a href="/react/docs/getting-started.html">Getting Started</a></li>
<li><a href="/react/docs/tutorial.html">Tutorial</a></li>
<li><a href="/react/docs/common-questions.html">Common Questions</a></li>
</ul>
</div>
<div class="nav-docs-section">
<h3>Reference</h3>
<ul>
<li><a href="/react/docs/syntax.html">JSX Syntax</a></li>
<li><a href="/react/docs/component-basics.html">Component Basics</a></li>
<li><a href="/react/docs/component-data.html">Component Data</a></li>
<li><a href="/react/docs/component-lifecycle.html">Component Lifecycle</a></li>
<li><a href="/react/docs/event-handling.html">Event Handling</a></li>
<li><a href="/react/docs/advanced-components.html" class="active">Advanced Components</a></li>
<li><a href="/react/docs/mixins.html">Mixins</a></li>
<li><a href="/react/docs/api.html">API</a></li>
</ul>
</div>
</div>
<div class="inner-content">
<h1>Advanced Components</h1>
<div class="subHeader">How to build advanced composite components.</div>
<p>Composite components extend a <code>ReactCompositeComponent</code> base class that provides
a very powerful API that makes React flexible and able to easily work with other
libraries and frameworks.</p>
<h2>Lifecycle Methods</h2>
<p>Composite components can optionally implement lifecycle methods that are invoked
at various stages in the <a href="component-lifecycle.html">component lifecycle</a> that
each have unique guarantees.</p>
<h3>Mounting</h3>
<ul>
<li><code>getInitialState(): object</code> is invoked before a component is mounted.
Stateful components should implement this and return the initial state data.</li>
<li><code>componentWillMount()</code> is invoked immediately before mounting occurs.</li>
<li><code>componentDidMount(DOMElement rootNode)</code> is invoked immediately after
mounting occurs. Initialization that requires DOM nodes should go here.</li>
</ul>
<h3>Updating</h3>
<ul>
<li><code>componentWillReceiveProps(object nextProps)</code> is invoked when a mounted
component receives new props. This method should be used to compare
<code>this.props</code> and <code>nextProps</code> to perform state transitions using
<code>this.setState()</code>.</li>
<li><code>shouldComponentUpdate(object nextProps, object nextState): boolean</code> is
invoked when a component decides whether any changes warrant an update to the
DOM. Implement this as an optimization to compare <code>this.props</code> with
<code>nextProps</code> and <code>this.state</code> with <code>nextState</code> and return false if React
should skip updating.</li>
<li><code>componentWillUpdate(object nextProps, object nextState)</code> is invoked
immediately before updating occurs. You cannot call <code>this.setState()</code> here.</li>
<li><code>componentDidUpdate(object prevProps, object prevState, DOMElement rootNode)</code>
is invoked immediately after updating occurs.</li>
</ul>
<h3>Unmounting</h3>
<ul>
<li><code>componentWillUnmount()</code> is invoked immediately before a component is
unmounted and destroyed. Cleanup should go here.</li>
</ul>
<h2>Mounted Methods</h2>
<p><em>Mounted</em> composite components also support the following methods:</p>
<ul>
<li><code>getDOMNode(): DOMElement</code> can be invoked on any mounted component in order
to obtain a reference to its rendered DOM node.</li>
<li><code>forceUpdate()</code> can be invoked on any mounted component when you know that
some deeper aspect of the component&#39;s state has changed without using
<code>this.setState()</code>.</li>
</ul>
<blockquote>
<p>Note:</p>
<p>The <code>DOMElement rootNode</code> argument of <code>componentDidMount()</code> and
<code>componentDidUpdate()</code> is a convenience. The same node can be obtained by
calling <code>this.getDOMNode()</code>.</p>
</blockquote>
<h2>Component Refs</h2>
<p>A common use case of event callbacks or the lifecycle methods is to operate on a
component returned by <code>render()</code>. For example, consider a search component that
should auto-focus the input once mounted:</p>
<div class="highlight"><pre><code class="javascript language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">SearchForm</span> <span class="o">=</span> <span class="nx">React</span><span class="p">.</span><span class="nx">createClass</span><span class="p">({</span>
<span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">(</span>
<span class="o">&lt;</span><span class="nx">form</span> <span class="nx">action</span><span class="o">=</span><span class="p">{</span><span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">action</span><span class="p">}</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">&quot;search&quot;</span> <span class="nx">placeholder</span><span class="o">=</span><span class="s2">&quot;Search...&quot;</span> <span class="o">/&gt;</span>
<span class="o">&lt;</span><span class="err">/form&gt;</span>
<span class="p">);</span>
<span class="p">},</span>
<span class="nx">componentDidMount</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">rootNode</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">searchInput</span> <span class="o">=</span> <span class="nx">rootNode</span><span class="p">.</span><span class="nx">firstChild</span><span class="p">;</span>
<span class="nx">searchInput</span><span class="p">.</span><span class="nx">focus</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">});</span>
</code></pre></div>
<p>Although this implementation works, it is fragile because <code>componentDidMount()</code>
now relies on <code>render()</code> returning a particular DOM structure.</p>
<p>React provides a better way for composite components to reference components
that it constructs in its <code>render()</code> method through the use of refs. A component
can assign a <code>ref</code> to any component it constructs. This will create a reference
to those components on <code>this.refs</code>. For example:</p>
<div class="highlight"><pre><code class="javascript language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">SearchForm</span> <span class="o">=</span> <span class="nx">React</span><span class="p">.</span><span class="nx">createClass</span><span class="p">({</span>
<span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">(</span>
<span class="o">&lt;</span><span class="nx">form</span> <span class="nx">action</span><span class="o">=</span><span class="p">{</span><span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">action</span><span class="p">}</span><span class="o">&gt;</span>
<span class="hll"> <span class="o">&lt;</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">&quot;search&quot;</span> <span class="nx">placeholder</span><span class="o">=</span><span class="s2">&quot;Search...&quot;</span> <span class="nx">ref</span><span class="o">=</span><span class="s2">&quot;searchInput&quot;</span> <span class="o">/&gt;</span>
</span> <span class="o">&lt;</span><span class="err">/form&gt;</span>
<span class="p">);</span>
<span class="p">},</span>
<span class="nx">componentDidMount</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">rootNode</span><span class="p">)</span> <span class="p">{</span>
<span class="hll"> <span class="kd">var</span> <span class="nx">searchInput</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">refs</span><span class="p">.</span><span class="nx">searchInput</span><span class="p">.</span><span class="nx">getDOMNode</span><span class="p">();</span>
</span> <span class="nx">searchInput</span><span class="p">.</span><span class="nx">focus</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">});</span>
</code></pre></div>
<p>In this example, <code>this.refs.searchInput</code> will reference the <code>&lt;input&gt;</code> component
and is available in most lifecycle methods and event callbacks. We obtain a
reference to the <code>&lt;input&gt;</code>&#39;s DOM node using <code>getDOMNode()</code>.</p>
<blockquote>
<p>Note:</p>
<p>If you want to preserve compatibility with Google Closure Compiler&#39;s
property crushing in <code>ADVANCED_OPTIMIZATIONS</code> mode, make sure to use string
literals with <code>this.refs</code>.</p>
</blockquote>
<div class="docs-prevnext">
<a class="docs-prev" href="/react/docs/event-handling.html">&larr; Prev</a>
<a class="docs-next" href="/react/docs/mixins.html">Next &rarr;</a>
</div>
</div>
</section>
<footer class="wrap">
<div class="left">A Facebook &amp; Instagram collaboration.</div>
<div class="right">&copy; 2013 Facebook Inc.</div>
</footer>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-41298772-1', 'facebook.github.io');
ga('send', 'pageview');
</script>
</body>
</html>