mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Rebuild website
This commit is contained in:
@@ -156,7 +156,7 @@
|
||||
|
||||
<p>This is not React-specific behavior; it is a part of <a href="https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/">how functions work in JavaScript</a>. Generally, if you refer to a method without <code>()</code> after it, such as <code>onClick={this.handleClick}</code>, you should bind that method.</p>
|
||||
|
||||
<p>If calling <code>bind</code> annoys you, there are two ways you can get around this. If you are using the experimental <a href="https://babeljs.io/docs/plugins/transform-class-properties/">property initializer syntax</a>, you can use property initializers to correctly bind callbacks:</p>
|
||||
<p>If calling <code>bind</code> annoys you, there are two ways you can get around this. If you are using the experimental <a href="https://babeljs.io/docs/plugins/transform-class-properties/">public class fields syntax</a>, you can use class fields to correctly bind callbacks:</p>
|
||||
<div class="highlight"><pre><code class="language-js" data-lang="js"><span class="kr">class</span> <span class="nx">LoggingButton</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">Component</span> <span class="p">{</span>
|
||||
<span class="hll"> <span class="c1">// This syntax ensures `this` is bound within handleClick.</span>
|
||||
</span><span class="hll"> <span class="c1">// Warning: this is *experimental* syntax.</span>
|
||||
@@ -175,7 +175,7 @@
|
||||
</code></pre></div>
|
||||
<p>This syntax is enabled by default in <a href="https://github.com/facebookincubator/create-react-app">Create React App</a>.</p>
|
||||
|
||||
<p>If you aren't using property initializer syntax, you can use an <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions">arrow function</a> in the callback:</p>
|
||||
<p>If you aren't using class fields syntax, you can use an <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions">arrow function</a> in the callback:</p>
|
||||
<div class="highlight"><pre><code class="language-js" data-lang="js"><span class="kr">class</span> <span class="nx">LoggingButton</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">Component</span> <span class="p">{</span>
|
||||
<span class="nx">handleClick</span><span class="p">()</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="s1">'this is:'</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span>
|
||||
@@ -191,7 +191,7 @@
|
||||
<span class="p">}</span>
|
||||
<span class="p">}</span>
|
||||
</code></pre></div>
|
||||
<p>The problem with this syntax is that a different callback is created each time the <code>LoggingButton</code> renders. In most cases, this is fine. However, if this callback is passed as a prop to lower components, those components might do an extra re-rendering. We generally recommend binding in the constructor or using the property initializer syntax, to avoid this sort of performance problem.</p>
|
||||
<p>The problem with this syntax is that a different callback is created each time the <code>LoggingButton</code> renders. In most cases, this is fine. However, if this callback is passed as a prop to lower components, those components might do an extra re-rendering. We generally recommend binding in the constructor or using the class fields syntax, to avoid this sort of performance problem.</p>
|
||||
|
||||
|
||||
<div class="docs-prevnext">
|
||||
|
||||
+1
-1
@@ -83,7 +83,7 @@
|
||||
<p>Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.</p>
|
||||
<div class="highlight"><pre><code class="language-js" data-lang="js"><span class="nx">ReactDOM</span><span class="p">.</span><span class="nx">createPortal</span><span class="p">(</span><span class="nx">child</span><span class="p">,</span> <span class="nx">container</span><span class="p">)</span>
|
||||
</code></pre></div>
|
||||
<p>The first argument (<code>child</code>) is any <a href="/docs/react-component.html#render">renderable React child</a>, such as an element, string, or fragment. The second argument (<code>container</code>) is a DOM element.</p>
|
||||
<p>The first argument (<code>child</code>) is any <a href="/react/docs/react-component.html#render">renderable React child</a>, such as an element, string, or fragment. The second argument (<code>container</code>) is a DOM element.</p>
|
||||
|
||||
<h2>Usage</h2>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user