mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
@@ -406,9 +406,9 @@
|
||||
|
||||
<p>If you'd like to use React on a touch device such as a phone or tablet, simply call <code>React.initializeTouchEvents(true);</code> to enable touch event handling.</p>
|
||||
<h2><a class="anchor" name="under-the-hood-autobinding-and-event-delegation"></a>Under the Hood: Autobinding and Event Delegation <a class="hash-link" href="#under-the-hood-autobinding-and-event-delegation">#</a></h2>
|
||||
<p>Under the hood React does a few things to keep your code performant and easy to understand.</p>
|
||||
<p>Under the hood, React does a few things to keep your code performant and easy to understand.</p>
|
||||
|
||||
<p><strong>Autobinding:</strong> When creating callbacks in JavaScript you usually need to explicitly bind a method to its instance such that the value of <code>this</code> is correct. With React, every method is automatically bound to its component instance. React caches the bound method such that it's extremely CPU and memory efficient. It's also less typing!</p>
|
||||
<p><strong>Autobinding:</strong> When creating callbacks in JavaScript, you usually need to explicitly bind a method to its instance such that the value of <code>this</code> is correct. With React, every method is automatically bound to its component instance. React caches the bound method such that it's extremely CPU and memory efficient. It's also less typing!</p>
|
||||
|
||||
<p><strong>Event delegation:</strong> React doesn't actually attach event handlers to the nodes themselves. When React starts up, it starts listening for all events at the top level using a single event listener. When a component is mounted or unmounted, the event handlers are simply added or removed from an internal mapping. When an event occurs, React knows how to dispatch it using this mapping. When there are no event handlers left in the mapping, React's event handlers are simple no-ops. To learn more about why this is fast, see <a href="http://davidwalsh.name/event-delegate">David Walsh's excellent blog post</a>.</p>
|
||||
<h2><a class="anchor" name="components-are-just-state-machines"></a>Components are Just State Machines <a class="hash-link" href="#components-are-just-state-machines">#</a></h2>
|
||||
|
||||
@@ -379,7 +379,7 @@
|
||||
</h1>
|
||||
<div class="subHeader"></div>
|
||||
|
||||
<p>If you know all the properties that you want to place on a component a head of time, it is easy to use JSX:</p>
|
||||
<p>If you know all the properties that you want to place on a component ahead of time, it is easy to use JSX:</p>
|
||||
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"> <span class="kd">var</span> <span class="nx">component</span> <span class="o">=</span> <span class="o"><</span><span class="nx">Component</span> <span class="nx">foo</span><span class="o">=</span><span class="p">{</span><span class="nx">x</span><span class="p">}</span> <span class="nx">bar</span><span class="o">=</span><span class="p">{</span><span class="nx">y</span><span class="p">}</span> <span class="o">/></span><span class="p">;</span>
|
||||
</code></pre></div><h2><a class="anchor" name="mutating-props-is-bad-mkay"></a>Mutating Props is Bad, mkay <a class="hash-link" href="#mutating-props-is-bad-mkay">#</a></h2>
|
||||
<p>If you don't know which properties you want to set, you might be tempted to add them onto the object later:</p>
|
||||
|
||||
@@ -400,7 +400,7 @@
|
||||
<span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">});</span>
|
||||
<span class="nx">React</span><span class="p">.</span><span class="nx">renderComponent</span><span class="p">(</span>
|
||||
<span class="nx">React</span><span class="p">.</span><span class="nx">render</span><span class="p">(</span>
|
||||
<span class="o"><</span><span class="nx">FancyCheckbox</span> <span class="nx">checked</span><span class="o">=</span><span class="p">{</span><span class="kc">true</span><span class="p">}</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">}</span><span class="o">></span>
|
||||
<span class="nx">Hello</span> <span class="nx">world</span><span class="o">!</span>
|
||||
<span class="o"><</span><span class="err">/FancyCheckbox>,</span>
|
||||
@@ -425,7 +425,7 @@
|
||||
<span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">});</span>
|
||||
<span class="nx">React</span><span class="p">.</span><span class="nx">renderComponent</span><span class="p">(</span>
|
||||
<span class="nx">React</span><span class="p">.</span><span class="nx">render</span><span class="p">(</span>
|
||||
<span class="o"><</span><span class="nx">FancyCheckbox</span> <span class="nx">checked</span><span class="o">=</span><span class="p">{</span><span class="kc">true</span><span class="p">}</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">}</span><span class="o">></span>
|
||||
<span class="nx">Hello</span> <span class="nx">world</span><span class="o">!</span>
|
||||
<span class="o"><</span><span class="err">/FancyCheckbox>,</span>
|
||||
|
||||
+1
-1
@@ -399,7 +399,7 @@
|
||||
<h3><a class="anchor" name="want-to-skip-all-this-and-just-see-the-source"></a>Want to skip all this and just see the source? <a class="hash-link" href="#want-to-skip-all-this-and-just-see-the-source">#</a></h3>
|
||||
<p><a href="https://github.com/reactjs/react-tutorial">It's all on GitHub.</a></p>
|
||||
<h3><a class="anchor" name="getting-started"></a>Getting started <a class="hash-link" href="#getting-started">#</a></h3>
|
||||
<p>For this tutorial we'll use prebuilt JavaScript files on a CDN. Open up your favorite editor and create a new HTML document:</p>
|
||||
<p>For this tutorial, we'll use prebuilt JavaScript files on a CDN. Open up your favorite editor and create a new HTML document:</p>
|
||||
<div class="highlight"><pre><code class="language-html" data-lang="html"><span class="c"><!-- template.html --></span>
|
||||
<span class="nt"><html></span>
|
||||
<span class="nt"><head></span>
|
||||
|
||||
Reference in New Issue
Block a user