React.js in patterns

Long time I was searching for a good front-end framework. Framework that will help me write scalable and easy to maintain UI. Even though React is just a library for rendering it comes with so many benefits that I can easily say “I found it”. And like every thing that I use a lot I started seeing some patterns. Techniques that are applied over and over again and I see in the code of other developers. It’s time that I start documenting, discussing and sharing these patterns.

All the techniques described here are available in github.com/krasimir/react-in-patterns repository. I’ll probably not update this blog post but will continue publishing stuff in GitHub. I’ll also encourage you to comment below, file an issue or directly make a pull request with patterns that you recognize.

Communication

Building with React for a couple of months and you’ll realize that every React component is like a small system that operates on its own. It has its own state, input and output.

Input

The input for a React component is its props. That’s how we pass data to it:

// Title.jsx
class Title extends React.Component {
  render() {
    return <h1>{ this.props.text }</h1>;
  }
};
Title.propTypes = {
  text: React.PropTypes.string
};
Title.defaultProps = {
  text: 'Hello world'
};

// App.jsx
class App extends React.Component {
  render() {
    return <Title text='Hello React' />;
  }
};

The Title component has only one input - text. The parent component (App) should provide it as an attribute while using the </code> tag. There are two additional settings that we see above:</p> <ul> <li><code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">propTypes</code> - defines the type of the props. This helps React telling us when a provided prop is not what we expect.</li> <li><code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">defaultProps</code> - defines the default values of the props. We may require the existence of certain props but for the rest is good practice to set a default value.</li> </ul> <p>There is also <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">props.children</code> property that gives us an access to the child components passed by the owner of the component. For example:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Title</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>text <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>children <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Title text<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token string" style="color:rgb(102,153,0)">'Hello React'</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>span<span class="token operator" style="color:rgb(166,127,89)">></span>community<span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>span<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>Title<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p><em>Notice that if we don’t return <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">{ this.props.children }</code> as part of the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Title</code>‘s render method the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)"><span></code> tag will not be rendered.</em></p> <p>An indirect input to a component may be also the so called <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">context</code>. The whole React tree may have a <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">context</code> object which is accessible by every component. More about that in the dependency injection section.</p> <h3 id="output" style="line-height:1.2em"><span class="headeranchor"></span>Output</h3> <p>The obvious output is the rendered HTML. Visually that’s what we get from a React component. Of course some of the components contain logic that probably sends out transformed data or triggers an action/event in our system. To achieve that we again use component’s props:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Title</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>a onClick<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>logoClicked <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>img src<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token string" style="color:rgb(102,153,0)">'path/to/logo.png'</span> <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>a<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Title logoClicked<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>logoClicked <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token function" style="color:rgb(221,74,104)">logoClicked<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> console<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">log<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token string" style="color:rgb(102,153,0)">'logo clicked'</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>We pass a callback which is invoked from within the component. The <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">logoClicked</code>function above may accept data which is how we transfer information back from the child to parent component.</p> <p>We should mention that there is no API that allow us accessing child’s state. Or in other words we can’t use <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">this.props.children[0].state</code> or something like that. The proper way of retrieving information from the children is by using props (passing callbacks). And that’s a good thing. This approach forces us defining clear APIs and encourage the one-way direction data flow.</p> <h2 id="composition" style="font-size:2em; line-height:1.2em"><span class="headeranchor"></span>Composition</h2> <blockquote style="font-size:0.9em; color:rgb(113,113,113); margin:1em 0px; padding:0px 0px 0px 1.2em; border-left:0.2em solid rgb(197,197,197)"> <p>Source code</p> </blockquote> <p>One of the biggest benefits of React is composability. I personally don’t know a framework that offers such an easy way to create and combine components. In this section we will explore few composition techniques which proved to work well.</p> <p>Let’s get a simple example. Let’s say that we have an application with a header and we want to place a navigation inside. We have three React components - <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">App</code>, <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Header</code>and <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Navigation</code>. They have to be nested into each other so we end up with the following markup:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token operator" style="color:rgb(166,127,89)"><</span>App<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Navigation<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>Navigation<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>Header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>App<span class="token operator" style="color:rgb(166,127,89)">></span> </code></code></pre> <p>The trivial approach for combining these components is to reference them in the places where we need them.</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token comment" style="color:rgb(112,128,144)">// app.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">import</span> Header from <span class="token string" style="color:rgb(102,153,0)">'./Header.jsx'</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Header <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token comment" style="color:rgb(112,128,144)">// Header.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">import</span> Navigation from <span class="token string" style="color:rgb(102,153,0)">'./Navigation.jsx'</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Header</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>header<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token operator" style="color:rgb(166,127,89)"><</span>Navigation <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span><span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>header<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token comment" style="color:rgb(112,128,144)">// Navigation.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Navigation</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span><span class="token operator" style="color:rgb(166,127,89)"><</span>nav<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>nav<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> </code></code></pre> <p>However, following this pattern we introduce several problems:</p> <ul> <li>We may consider the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">App</code> as a place where we wire stuff, as an entry point. So, it’s a good place for such composition. The <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Header</code> though may have other elements like a logo, search field or a slogan. It will be nice if they are passed somehow from the outside so we don’t create a hard-coded dependency. What if we need the same <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Header</code> component but without the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Navigation</code>. We can’t easily achieve that because we have the two bound tightly together.</li> <li>It’s difficult to test. We may have some business logic in the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Header</code> and in order to test it we have to create an instance of the component. However, because it imports other components we will probably create instances of those components too and it becomes heavy for testing. We may break our <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Header</code> test by doing something wrong in the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Navigation</code> component which is totally misleading. <em>(Note: while testing the shallow rendering solves this problem by rendering only the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Header</code> without its nested children.)</em></li> </ul> <h3 id="using-reacts-children-api" style="line-height:1.2em"><span class="headeranchor"></span>Using React’s children API</h3> <p>In React we have the handy <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">this.props.children</code>. That’s how the parent reads/accesses its children. This API will make our Header agnostic and dependency-free:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token comment" style="color:rgb(112,128,144)">// App.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Navigation <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>Header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token comment" style="color:rgb(112,128,144)">// Header.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Header</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>header<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>children <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>header<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>It’s also easy to test because we may render the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Header</code> with an empty <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)"><div></code>. This will isolate the component and will let us focus on only one piece of our application.</p> <h3 id="passing-a-child-as-a-property" style="line-height:1.2em"><span class="headeranchor"></span>Passing a child as a property</h3> <p>Every React component receive props. It’s nice that these props may contain all kind of data. Even other components.</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token comment" style="color:rgb(112,128,144)">// App.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">var</span> title <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span>Hello there<span class="token operator" style="color:rgb(166,127,89)">!</span><span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Header title<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> title <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Navigation <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>Header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token comment" style="color:rgb(112,128,144)">// Header.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Header</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>title <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>hr <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>children <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>This technique is helpful when we have a mix between components that exist inside the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Header</code> and components that have to be provided from the outside.</p> <h2 id="higher-order-components" style="font-size:2em; line-height:1.2em"><span class="headeranchor"></span>Higher-order components</h2> <blockquote style="font-size:0.9em; color:rgb(113,113,113); margin:1em 0px; padding:0px 0px 0px 1.2em; border-left:0.2em solid rgb(197,197,197)"> <p>Source code</p> </blockquote> <p>Higher-order components look really similar to the decorator design pattern. It is wrapping a component and attaching some new functionalities or props to it.</p> <p>Here is a function that returns a higher-order component:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">var</span> enhanceComponent <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span>Component<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Enhance</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Component <span class="token punctuation" style="color:rgb(153,153,153)">{</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state<span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> enhanceComponent<span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>Very often we expose a factory function that accepts our original component and when called returns the enhanced/wrapped version of it. For example:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">var</span> OriginalComponent <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>p<span class="token operator" style="color:rgb(166,127,89)">></span>Hello world<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>p<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">createElement<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token function" style="color:rgb(221,74,104)">enhanceComponent<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>OriginalComponent<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>The very first thing that the higher-order component does is to render the original component. It’s also a good practice to pass the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">state</code> and <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">props</code> to it. This is helpful when we want to proxy data and use the higher-order component as it is our original component.</p> <p>The higher-order component gives us control on the input. The data that we want to send as props. Let’s say that we have a configuration setting that <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">OriginalComponent</code>needs:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">var</span> config <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token function" style="color:rgb(221,74,104)">require<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token string" style="color:rgb(102,153,0)">'path/to/configuration'</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">var</span> enhanceComponent <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span>Component<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Enhance</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Component <span class="token punctuation" style="color:rgb(153,153,153)">{</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state<span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">}</span> title<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> config<span class="token punctuation" style="color:rgb(153,153,153)">.</span>appTitle <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>The knowledge for the configuration is hidden into the higher-order component. <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">OriginalComponent</code> knows only that it receives a <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">prop</code> called <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">title</code>. Where it comes from it is not important. That’s a huge advantage because it helps us testing the component in an isolation and provides nice mechanism for mocking. Here is how the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">title</code> may be used:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">var</span> OriginalComponent <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>p<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>title <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>p<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>Higher-order components are involved into another useful pattern - dependency injection.</p> <h2 id="dependency-injection" style="font-size:2em; line-height:1.2em"><span class="headeranchor"></span>Dependency injection</h2> <blockquote style="font-size:0.9em; color:rgb(113,113,113); margin:1em 0px; padding:0px 0px 0px 1.2em; border-left:0.2em solid rgb(197,197,197)"> <p>Source code</p> </blockquote> <p>Big part of the modules/components that we write have dependencies. A proper management of these dependencies is critical for the success of the project. There is a technique (some people consider it as a <em>pattern</em>) called <em>dependency injection</em> that helps solving the problem.</p> <p>In React the need of dependency injector is easily visible. Let’s consider the following application tree:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token comment" style="color:rgb(112,128,144)">// Title.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span> <span class="token function" style="color:rgb(221,74,104)">Title<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>title <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token comment" style="color:rgb(112,128,144)">// Header.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">import</span> Title from <span class="token string" style="color:rgb(102,153,0)">'./Title.jsx'</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span> <span class="token function" style="color:rgb(221,74,104)">Header<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Title <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token comment" style="color:rgb(112,128,144)">// App.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">import</span> Header from <span class="token string" style="color:rgb(102,153,0)">'./Header.jsx'</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">constructor<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">super</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> title<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token string" style="color:rgb(102,153,0)">'React in patterns'</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Header <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>The string “React in patterns” should somehow reach the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Title</code> component. The direct way of doing this is to pass it from <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">App</code> to <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Header</code> and then <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Header</code> to pass it to <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Title</code>. However, this may work for these three components but what happens if there are multiple properties and deeper nesting. Lots of components will have to mention properties that they are not interested in.</p> <p>We already saw how the higher-order component may be used to inject data. Let’s use the same technique to inject the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">title</code> variable:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token comment" style="color:rgb(112,128,144)">// enhance.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">var</span> title <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token string" style="color:rgb(102,153,0)">'React in patterns'</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">var</span> enhanceComponent <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span>Component<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Enhance</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Component <span class="token punctuation" style="color:rgb(153,153,153)">{</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state<span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">}</span> title<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> title <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token comment" style="color:rgb(112,128,144)">// Header.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">import</span> enhance from <span class="token string" style="color:rgb(102,153,0)">'./enhance.jsx'</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">import</span> Title from <span class="token string" style="color:rgb(102,153,0)">'./Title.jsx'</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">var</span> EnhancedTitle <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token function" style="color:rgb(221,74,104)">enhance<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>Title<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span> <span class="token function" style="color:rgb(221,74,104)">Header<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>EnhancedTitle <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>header<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> </code></code></pre> <p>The <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">title</code> is hidden in a middle layer (higher-order component) where we pass it as a prop to the original <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Title</code> component. That’s all nice but it solves only half of the problem. Now we don’t have to pass the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">title</code> down the tree but how this data will reach the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">enhance.jsx</code> helper.</p> <p>React has the concept of <em>context</em>. The <em>context</em> is something that every component may have access to. It’s something like an event bus but for data. A single model which we can access from everywhere.</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token comment" style="color:rgb(112,128,144)">// a place where we'll define the context </span><span class="token keyword" style="color:rgb(0,119,170)">var</span> context <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> title<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token string" style="color:rgb(102,153,0)">'React in patterns'</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">getChildContext<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> context<span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> App<span class="token punctuation" style="color:rgb(153,153,153)">.</span>childContextTypes <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> title<span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>string <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token comment" style="color:rgb(112,128,144)">// a place where we need data </span><span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Inject</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">var</span> title <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>context<span class="token punctuation" style="color:rgb(153,153,153)">.</span>title<span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> Inject<span class="token punctuation" style="color:rgb(153,153,153)">.</span>contextTypes <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> title<span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>string <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>Notice that we have to specify the exact signature of the context object. With <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">childContextTypes</code> and <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">contextTypes</code>. If those are not specified then the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">context</code> object will be empty. That may be a little bit frustrating because we may have lots of stuff to put there. That’s why it is a good practice that our <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">context</code> is not just a plain object but it has an interface that allows us to store and retrieve data. For example:</p> <p></p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token comment" style="color:rgb(112,128,144)">// dependencies.js </span><span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> data<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span><span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token keyword" style="color:rgb(0,119,170)">get</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span>key<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>data<span class="token punctuation" style="color:rgb(153,153,153)">[</span>key<span class="token punctuation" style="color:rgb(153,153,153)">]</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token function" style="color:rgb(221,74,104)">register<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>key<span class="token punctuation" style="color:rgb(153,153,153)">,</span> value<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>data<span class="token punctuation" style="color:rgb(153,153,153)">[</span>key<span class="token punctuation" style="color:rgb(153,153,153)">]</span> <span class="token operator" style="color:rgb(166,127,89)">=</span> value<span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> </code></code></pre> Then, if we go back to our example, the very top  <code>App</code> component may look like that: <p></p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">import</span> dependencies from <span class="token string" style="color:rgb(102,153,0)">'./dependencies'</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> dependencies<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">register<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token string" style="color:rgb(102,153,0)">'title'</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token string" style="color:rgb(102,153,0)">'React in patterns'</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">getChildContext<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> dependencies<span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Header <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> App<span class="token punctuation" style="color:rgb(153,153,153)">.</span>childContextTypes <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> data<span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>object<span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token keyword" style="color:rgb(0,119,170)">get</span><span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>func<span class="token punctuation" style="color:rgb(153,153,153)">,</span> register<span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>func <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>And our <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Title</code> component gets it’s data through the context:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token comment" style="color:rgb(112,128,144)">// Title.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Title</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>context<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">get</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span><span class="token string" style="color:rgb(102,153,0)">'title'</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> Title<span class="token punctuation" style="color:rgb(153,153,153)">.</span>contextTypes <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> data<span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>object<span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token keyword" style="color:rgb(0,119,170)">get</span><span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>func<span class="token punctuation" style="color:rgb(153,153,153)">,</span> register<span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>func <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>Ideally we don’t want to specify the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">contextTypes</code> every time when we need an access to the context. This detail may be wrapped in a higher-order component. And even more, we may write an utility function that is more descriptive and helps us declare the exact wiring. I.e instead of accessing the context directly with <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">this.context.get('title')</code>we ask the higher-order component to get what we need and to pass it as a prop to our component. For example:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token comment" style="color:rgb(112,128,144)">// Title.jsx </span><span class="token keyword" style="color:rgb(0,119,170)">import</span> wire from <span class="token string" style="color:rgb(102,153,0)">'./wire'</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span> <span class="token function" style="color:rgb(221,74,104)">Title<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>title <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>h1<span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token function" style="color:rgb(221,74,104)">wire<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>Title<span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token punctuation" style="color:rgb(153,153,153)">[</span><span class="token string" style="color:rgb(102,153,0)">'title'</span><span class="token punctuation" style="color:rgb(153,153,153)">]</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span> <span class="token function" style="color:rgb(221,74,104)">resolve<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>title<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> title <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>The <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">wire</code> function accepts first a React component, then an array with all the needed dependencies (which are <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">register</code>ed already) and then a function which I like to call <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">mapper</code>. It receives what’s stored in the context as a raw data and returns an object which is the actual React props for our component (<code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Title</code>). In this example we just pass what we get - a <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">title</code> string variable. However, in a real app this could be a collection of data stores, configuration or something else. So, it’s nice that we pass exactly what we need and don’t pollute the components with data that they don’t need.</p> <p>Here is how the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">wire</code> function looks like:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">export</span> <span class="token keyword" style="color:rgb(0,119,170)">default</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span> <span class="token function" style="color:rgb(221,74,104)">wire<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>Component<span class="token punctuation" style="color:rgb(153,153,153)">,</span> dependencies<span class="token punctuation" style="color:rgb(153,153,153)">,</span> mapper<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Inject</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">var</span> resolved <span class="token operator" style="color:rgb(166,127,89)">=</span> dependencies<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">map<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>context<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">get</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">bind<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>context<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">var</span> props <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token function" style="color:rgb(221,74,104)">mapper<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>resolved<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">createElement<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>Component<span class="token punctuation" style="color:rgb(153,153,153)">,</span> props<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> Inject<span class="token punctuation" style="color:rgb(153,153,153)">.</span>contextTypes <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> data<span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>object<span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token keyword" style="color:rgb(0,119,170)">get</span><span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>func<span class="token punctuation" style="color:rgb(153,153,153)">,</span> register<span class="token punctuation" style="color:rgb(153,153,153)">:</span> React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>PropTypes<span class="token punctuation" style="color:rgb(153,153,153)">.</span>func <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> Inject<span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p><code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Inject</code> is a higher-order component that gets access to the context and retrieves all the items listed under <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">dependencies</code> array. The <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">mapper</code> is a function receiving the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">context</code> data and transforms it to props for our component.</p> <h3 id="final-thoughts-about-dependency-injection" style="line-height:1.2em"><span class="headeranchor"></span>Final thoughts about dependency injection</h3> <p>Most of the solutions for dependency injection in React components are based on context. I think that it’s good to know what happens under the hood. As the time of this writing one of the most popular ways for building React apps involves Redux. The<em>famous</em> <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">connect</code> function and the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Provider</code> there use the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">context</code>.</p> <p>I personally found this technique really useful. It successfully fullfills my dependencies needs and makes my components pure and highly testable.</p> <h2 id="one-way-direction-data-flow" style="font-size:2em; line-height:1.2em"><span class="headeranchor"></span>One-way direction data flow</h2> <blockquote style="font-size:0.9em; color:rgb(113,113,113); margin:1em 0px; padding:0px 0px 0px 1.2em; border-left:0.2em solid rgb(197,197,197)"> <p>Source code</p> </blockquote> <p>One-way direction data flow is a pattern that works nicely with React. It is around the idea that the components do not modify the data that they receive. They only listen for changes in this data and maybe provide the new value but they do not update the actual data store. This update happens following another mechanism in another place and the component just gets rendered with the new value.</p> <p>Let’s for example get a simple <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Switcher</code> component that contains a button. We click it to enable a flag in the system.</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Switcher</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">constructor<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">super</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> flag<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token keyword" style="color:rgb(0,119,170)">false</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_onButtonClick <span class="token operator" style="color:rgb(166,127,89)">=</span> e <span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">setState<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> flag<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token operator" style="color:rgb(166,127,89)">!</span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state<span class="token punctuation" style="color:rgb(153,153,153)">.</span>flag <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>button onClick<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_onButtonClick <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state<span class="token punctuation" style="color:rgb(153,153,153)">.</span>flag <span class="token operator" style="color:rgb(166,127,89)">?</span> <span class="token string" style="color:rgb(102,153,0)">'lights on'</span> <span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token string" style="color:rgb(102,153,0)">'lights off'</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>button<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token comment" style="color:rgb(112,128,144)">// ... and we render it </span><span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Switcher <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>At this moment we have the data inside our component. Or in other words, <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Switcher</code> is the only one place that knows about our <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">flag</code>. Let’s send it out to some kind of a store:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">var</span> Store <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> _flag<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token keyword" style="color:rgb(0,119,170)">false</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token keyword" style="color:rgb(0,119,170)">set</span><span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span>value<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_flag <span class="token operator" style="color:rgb(166,127,89)">=</span> value<span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token keyword" style="color:rgb(0,119,170)">get</span><span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_flag<span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Switcher</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">constructor<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">super</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> flag<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token keyword" style="color:rgb(0,119,170)">false</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_onButtonClick <span class="token operator" style="color:rgb(166,127,89)">=</span> e <span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">setState<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> flag<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token operator" style="color:rgb(166,127,89)">!</span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state<span class="token punctuation" style="color:rgb(153,153,153)">.</span>flag <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">onChange<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state<span class="token punctuation" style="color:rgb(153,153,153)">.</span>flag<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>button onClick<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_onButtonClick <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state<span class="token punctuation" style="color:rgb(153,153,153)">.</span>flag <span class="token operator" style="color:rgb(166,127,89)">?</span> <span class="token string" style="color:rgb(102,153,0)">'lights on'</span> <span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token string" style="color:rgb(102,153,0)">'lights off'</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>button<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Switcher onChange<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> Store<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">set</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">bind<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>Store<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>Our <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Store</code> object is a simple singleton where we have helpers for setting and getting the value of the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">_flag</code> property. By passing the getter to the component we are able to update the data externally. More or less our application workflow looks like that:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em">User's input <span class="token operator" style="color:rgb(166,127,89)">|</span> Switcher <span class="token operator" style="color:rgb(166,127,89)">--</span><span class="token operator" style="color:rgb(166,127,89)">--</span><span class="token operator" style="color:rgb(166,127,89)">--</span><span class="token operator" style="color:rgb(166,127,89)">-</span><span class="token operator" style="color:rgb(166,127,89)">></span> Store </code></code></pre> <p>Let’s assume that we are saving the flag value to a backend service via the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Store</code>. When the user comes back we have to set a proper initial state. If the user left the flag truthy we have to show <em>“lights on”</em> and not the default <em>“lights off”</em>. Now it gets tricky because we have the data knowledge in two places. The UI and the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Store</code> have their own states. We have to communicate in both directions <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Store ---> Switcher</code> and <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Switcher ---> Store</code>.</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token comment" style="color:rgb(112,128,144)">// ... in App component </span><span class="token operator" style="color:rgb(166,127,89)"><</span>Switcher value<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> Store<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">get</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> onChange<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> Store<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">set</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">bind<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>Store<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token comment" style="color:rgb(112,128,144)">// ... in Switcher component </span><span class="token function" style="color:rgb(221,74,104)">constructor<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">super</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>state <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> flag<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>value <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span> </code></code></pre> <p>Our schema changes to the following:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em">User's input <span class="token operator" style="color:rgb(166,127,89)">|</span> Switcher <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">--</span><span class="token operator" style="color:rgb(166,127,89)">--</span><span class="token operator" style="color:rgb(166,127,89)">--</span><span class="token operator" style="color:rgb(166,127,89)">-</span><span class="token operator" style="color:rgb(166,127,89)">></span> Store <span class="token operator" style="color:rgb(166,127,89)">^</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> v Service communicating <span class="token keyword" style="color:rgb(0,119,170)">with</span> our backend </code></code></pre> <p>All this leads to managing two states instead of one. What if the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Store</code> changes its value based on other actions in the system. We have to propagate that change to the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Switcher</code> and we increase the complexity of our app.</p> <p>One-way direction data flow solves this problem. It eliminates the multiple states and deals with only one which is usually inside the store. To achieve that we have to tweak our <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Store</code> object a little bit. We need logic that allows us to subscribe for changes:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">var</span> Store <span class="token operator" style="color:rgb(166,127,89)">=</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> _handlers<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token punctuation" style="color:rgb(153,153,153)">[</span><span class="token punctuation" style="color:rgb(153,153,153)">]</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> _flag<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token string" style="color:rgb(102,153,0)">''</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> onChange<span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span>handler<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_handlers<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">push<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>handler<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token keyword" style="color:rgb(0,119,170)">set</span><span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span>value<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_flag <span class="token operator" style="color:rgb(166,127,89)">=</span> value<span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_handlers<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">forEach<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>handler <span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token function" style="color:rgb(221,74,104)">handler<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">,</span> <span class="token keyword" style="color:rgb(0,119,170)">get</span><span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token keyword" style="color:rgb(0,119,170)">function</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_flag<span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>Then we will hook our main <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">App</code> component and we’ll re-render it every time when the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Store</code> changes its value:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">App</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">constructor<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">super</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> Store<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">onChange<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>forceUpdate<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">bind<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>div<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span>Switcher value<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> Store<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">get</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> onChange<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> Store<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token keyword" style="color:rgb(0,119,170)">set</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">bind<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>Store<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)">/</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>div<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p><em>(Notice that we are using <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">forceUpdate</code> which is not really recommended. Normally ahigh-order component is used to enable the re-rendering. We used <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">forceUpdate</code> just to keep the example simple.)</em></p> <p>Because of this change the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Switcher</code> becomes really simple. We don’t need the internal state:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em"><span class="token keyword" style="color:rgb(0,119,170)">class</span> <span class="token class-name">Switcher</span> <span class="token keyword" style="color:rgb(0,119,170)">extends</span> <span class="token class-name">React<span class="token punctuation" style="color:rgb(153,153,153)">.</span>Component</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token function" style="color:rgb(221,74,104)">constructor<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">super</span><span class="token punctuation" style="color:rgb(153,153,153)">(</span>props<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_onButtonClick <span class="token operator" style="color:rgb(166,127,89)">=</span> e <span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span><span class="token function" style="color:rgb(221,74,104)">onChange<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token operator" style="color:rgb(166,127,89)">!</span><span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>value<span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token function" style="color:rgb(221,74,104)">render<span class="token punctuation" style="color:rgb(153,153,153)">(</span></span><span class="token punctuation" style="color:rgb(153,153,153)">)</span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">return</span> <span class="token punctuation" style="color:rgb(153,153,153)">(</span> <span class="token operator" style="color:rgb(166,127,89)"><</span>button onClick<span class="token operator" style="color:rgb(166,127,89)">=</span><span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>_onButtonClick <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">{</span> <span class="token keyword" style="color:rgb(0,119,170)">this</span><span class="token punctuation" style="color:rgb(153,153,153)">.</span>props<span class="token punctuation" style="color:rgb(153,153,153)">.</span>value <span class="token operator" style="color:rgb(166,127,89)">?</span> <span class="token string" style="color:rgb(102,153,0)">'lights on'</span> <span class="token punctuation" style="color:rgb(153,153,153)">:</span> <span class="token string" style="color:rgb(102,153,0)">'lights off'</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">/</span>button<span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token punctuation" style="color:rgb(153,153,153)">)</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span> <span class="token punctuation" style="color:rgb(153,153,153)">}</span><span class="token punctuation" style="color:rgb(153,153,153)">;</span> </code></code></pre> <p>The benefit that comes with this pattern is that our components become dummy representation of the <code style="font-family:inherit; display:inline-block; font-size:0.8em; padding:0px 0.4em; background:rgb(241,241,241)">Store</code>‘s data. It’s really easy to think about the React components as views (renderers). We write our application in a declarative way and deal with the complexity in only one place.</p> <p>The diagram of the application changes to:</p> <pre><code class="language-javascript"><code class=" language-javascript" style="font-family:Consolas,Monaco,"Andale Mono",monospace; direction:ltr; word-spacing:normal; word-break:normal; font-size:0.9em; line-height:1.1em">Service communicating <span class="token keyword" style="color:rgb(0,119,170)">with</span> our backend <span class="token operator" style="color:rgb(166,127,89)">^</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> v Store <span class="token operator" style="color:rgb(166,127,89)"><</span><span class="token operator" style="color:rgb(166,127,89)">--</span><span class="token operator" style="color:rgb(166,127,89)">--</span><span class="token operator" style="color:rgb(166,127,89)">-</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> v <span class="token operator" style="color:rgb(166,127,89)">|</span> Switcher <span class="token operator" style="color:rgb(166,127,89)">--</span><span class="token operator" style="color:rgb(166,127,89)">--</span><span class="token operator" style="color:rgb(166,127,89)">></span> <span class="token operator" style="color:rgb(166,127,89)">^</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> <span class="token operator" style="color:rgb(166,127,89)">|</span> User input </code></code></pre> <p>As we can see the data flows in only one direction and there is no need to sync two (or more) parts of our system. One-way direction data flow is not only about React based apps. Proved many times that makes the applications easy to reason about. It may need a little bit more wiring but it definitely worth it.</p> <h2 id="summary" style="font-size:2em; line-height:1.2em"><span class="headeranchor"></span>Summary</h2> <p>For sure, these are not the all design patterns/techniques that we can see in React. There are probably dozens more. Check out github.com/krasimir/react-in-patternsregularly for updates. I’ll try posting my new findings there.</p> </div> </div> </div> </div> </div> <!--PC和WAP自适应版--> <div id="SOHUCS" sid="1275786957762347008"></div> <script type="text/javascript" src="/views/front/js/chanyan.js"></script> <!-- 文章页-底部 动态广告位 --> <div class="youdao-fixed-ad" id="detail_ad_bottom"></div> </div> <div class="col-md-3"> <div class="row" id="ad"> <!-- 文章页-右侧1 动态广告位 --> <div id="right-1" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_1"> </div> </div> <!-- 文章页-右侧2 动态广告位 --> <div id="right-2" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_2"></div> </div> <!-- 文章页-右侧3 动态广告位 --> <div id="right-3" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_3"></div> </div> </div> </div> </div> </div> </div> <div class="container"> <h4 class="pt20 mb15 mt0 border-top">你可能感兴趣的:(React)</h4> <div id="paradigm-article-related"> <div class="recommend-post mb30"> <ul class="widget-links"> <li><a href="/article/1835492740536823808.htm" title="node.js学习" target="_blank">node.js学习</a> <span class="text-muted">小猿L</span> <a class="tag" taget="_blank" href="/search/node.js/1.htm">node.js</a><a class="tag" taget="_blank" href="/search/node.js/1.htm">node.js</a><a class="tag" taget="_blank" href="/search/%E5%AD%A6%E4%B9%A0/1.htm">学习</a><a class="tag" taget="_blank" href="/search/vim/1.htm">vim</a> <div>node.js学习实操及笔记温故node.js,node.js学习实操过程及笔记~node.js学习视频node.js官网node.js中文网实操笔记githubcsdn笔记为什么学node.js可以让别人访问我们编写的网页为后续的框架学习打下基础,三大框架vuereactangular离不开node.jsnode.js是什么官网:node.js是一个开源的、跨平台的运行JavaScript的运行</div> </li> <li><a href="/article/1835448111909138432.htm" title="react-intl——react国际化使用方案" target="_blank">react-intl——react国际化使用方案</a> <span class="text-muted">苹果酱0567</span> <a class="tag" taget="_blank" href="/search/%E9%9D%A2%E8%AF%95%E9%A2%98%E6%B1%87%E6%80%BB%E4%B8%8E%E8%A7%A3%E6%9E%90/1.htm">面试题汇总与解析</a><a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a><a class="tag" taget="_blank" href="/search/%E4%B8%AD%E9%97%B4%E4%BB%B6/1.htm">中间件</a><a class="tag" taget="_blank" href="/search/spring/1.htm">spring</a><a class="tag" taget="_blank" href="/search/boot/1.htm">boot</a><a class="tag" taget="_blank" href="/search/%E5%90%8E%E7%AB%AF/1.htm">后端</a> <div>国际化介绍i18n:internationalization国家化简称,首字母+首尾字母间隔的字母个数+尾字母,类似的还有k8s(Kubernetes)React-intl是React中最受欢迎的库。使用步骤安装#usenpmnpminstallreact-intl-D#useyarn项目入口文件配置//index.tsximportReactfrom"react";importReactDOMf</div> </li> <li><a href="/article/1835411044768509952.htm" title="字节二面" target="_blank">字节二面</a> <span class="text-muted">Redstone Monstrosity</span> <a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/%E9%9D%A2%E8%AF%95/1.htm">面试</a> <div>1.假设你是正在面试前端开发工程师的候选人,面试官让你详细说出你上一段实习过程的收获和感悟。在上一段实习过程中,我获得了宝贵的实践经验和深刻的行业洞察,以下是我的主要收获和感悟:一、专业技能提升框架应用熟练度:通过实际项目,我深入掌握了React、Vue等前端框架的使用,不仅提升了编码效率,还学会了如何根据项目需求选择合适的框架。问题解决能力:在实习期间,我遇到了许多预料之外的技术难题。通过查阅文</div> </li> <li><a href="/article/1835398064727224320.htm" title="前端代码上传文件" target="_blank">前端代码上传文件</a> <span class="text-muted">余生逆风飞翔</span> <a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a> <div>点击上传文件import{ElNotification}from'element-plus'import{API_CONFIG}from'../config/index.js'import{UploadFilled}from'@element-plus/icons-vue'import{reactive}from'vue'import{BASE_URL}from'../config/index'i</div> </li> <li><a href="/article/1835133874892533760.htm" title="前端使用react-intl-universal进行国际化" target="_blank">前端使用react-intl-universal进行国际化</a> <span class="text-muted">Stephy_Yy</span> <a class="tag" taget="_blank" href="/search/%23/1.htm">#</a><a class="tag" taget="_blank" href="/search/%E8%B0%83%E7%A0%94/1.htm">调研</a><a class="tag" taget="_blank" href="/search/reactjs/1.htm">reactjs</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/css/1.htm">css</a> <div>一、国际化/i18n目前国际化,就是开发者写对象,一个key关联若干语种的翻译。相比于浏览器自带的翻译功能,语义更加准确。“国际化”的简称:i18n(其来源是英文单词internationalization的首末字符i和n,18为中间的字符数)二、react项目国际化react-intl是业界最受欢迎的软件包之一:React-intl是雅虎的语言国际化开源项目FormatJS的一部分,通过其提供的</div> </li> <li><a href="/article/1835101722159181824.htm" title="VUE实现大小缩放轮播图" target="_blank">VUE实现大小缩放轮播图</a> <span class="text-muted">书边事.</span> <a class="tag" taget="_blank" href="/search/vue.js/1.htm">vue.js</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a> <div>效果图import{ref,computed,reactive,watch,onMounted}from'vue';exportdefault{props:{/***轮播数据来源*/source:{type:Array,default:()=>[{img:require('@/assets/imgs/test/1.png')},{img:require('@/assets/imgs/test/1.</div> </li> <li><a href="/article/1835089183857799168.htm" title="react里的index.js是怎么跟index.html结合起来的?" target="_blank">react里的index.js是怎么跟index.html结合起来的?</a> <span class="text-muted">SherrinfordL</span> <div>image.pngcreate-react-app把webpack、babel等配置都封装到了依赖项目react-script中,所以你无法直观的看到这些配置。你可以在项目下运行npmruneject,被隐藏的配置文件就会暴露到项目根路径下。把请求转发到index.html原因是,你执行npmrunstart时,启动的webpack-dev-server,会加载react-script项目conf</div> </li> <li><a href="/article/1835085841114951680.htm" title="Taro实现微信小程序自定义拍照截图识别" target="_blank">Taro实现微信小程序自定义拍照截图识别</a> <span class="text-muted">书边事.</span> <a class="tag" taget="_blank" href="/search/taro/1.htm">taro</a><a class="tag" taget="_blank" href="/search/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/1.htm">微信小程序</a><a class="tag" taget="_blank" href="/search/%E5%B0%8F%E7%A8%8B%E5%BA%8F/1.htm">小程序</a> <div>效果图:代码:>请对准框内拍摄题目重新拍照文件处理中...开始识别definePageConfig({navigationStyle:"custom",navigationBarTitleText:"",//启用页面分享//enableShareAppMessage:true,//启动朋友圈分享//enableShareTimeline:true});import{reactive,toRefs,</div> </li> <li><a href="/article/1835053432449363968.htm" title="(小白入门)Windows环境下搭建React Native Android开发环境" target="_blank">(小白入门)Windows环境下搭建React Native Android开发环境</a> <span class="text-muted">码农老黑</span> <a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/React/1.htm">React</a><a class="tag" taget="_blank" href="/search/Native/1.htm">Native</a><a class="tag" taget="_blank" href="/search/%E7%A7%BB%E5%8A%A8%E5%BC%80%E5%8F%91/1.htm">移动开发</a><a class="tag" taget="_blank" href="/search/Android/1.htm">Android</a><a class="tag" taget="_blank" href="/search/studio/1.htm">studio</a> <div>ReactNative(简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架,是Facebook早先开源的UI框架React在原生移动应用平台的衍生产物,目前支持iOS和Android两大平台。RN的环境搭建在RN的中文社区有所介绍,但是对于小白来说还是有些太过简略了。RN中文社区详见参考,本文不涉及的问题也许在其中能够有所解答。ReactNative思想底层引擎是JavaSc</div> </li> <li><a href="/article/1834965176286277632.htm" title="react中如何获取并使用usestate声明的变量的值" target="_blank">react中如何获取并使用usestate声明的变量的值</a> <span class="text-muted">小华0000</span> <a class="tag" taget="_blank" href="/search/react.js/1.htm">react.js</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a> <div>1.函数式更新当需要根据当前状态来更新状态时,可以使用函数式更新。setState(在类组件中)和setCount(在useState中)都可以接受一个函数作为参数,这个函数接收当前的状态作为参数,并返回新的状态。functionExampleComponent(){const[count,setCount]=useState(0);//使用函数式更新functionhandleClick(){s</div> </li> <li><a href="/article/1834963412711469056.htm" title="【Vue3源码实现】Ref isRef unRef proxyRefs实现" target="_blank">【Vue3源码实现】Ref isRef unRef proxyRefs实现</a> <span class="text-muted">ZhaiMou</span> <a class="tag" taget="_blank" href="/search/vue.js/1.htm">vue.js</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/1.htm">数据结构</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF%E6%A1%86%E6%9E%B6/1.htm">前端框架</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a> <div>前言在上篇文章中我们了解了响应式原理,并在最后实现了reactive。上文链接Vue3响应式原理实现与track和trigger依赖收集和触发依赖在我们的日常业务中,我们有可能需要将一个基础数据类型的值也转换成响应式的,而reactive只能代理对象,我们需要对基础数据类型的值也进行读写操作的拦截,但Proxy无法实现对基础数据类型值读写操作的拦截。所以Vue设计了Ref,以及相关api本篇文章实</div> </li> <li><a href="/article/1834963034653683712.htm" title="React 前端应用结合 Nginx 部署指南及常见错误排查" target="_blank">React 前端应用结合 Nginx 部署指南及常见错误排查</a> <span class="text-muted">蜗牛去旅行吧</span> <a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/react.js/1.htm">react.js</a><a class="tag" taget="_blank" href="/search/nginx/1.htm">nginx</a> <div>在现代Web开发中,React已成为构建用户界面的流行选择,而Nginx则是一个高性能的Web服务器,广泛用于静态文件的托管和负载均衡。在本篇博客中,我们将详细介绍如何将一个React应用部署到Nginx上,并探讨在部署过程中可能遇到的常见错误及其解决方案。部署步骤1.准备React应用首先,确保你已经创建了一个React应用。如果还没有,可以使用CreateReactApp快速生成一个基础项目:</div> </li> <li><a href="/article/1834961772746993664.htm" title="vue3 响应性API" target="_blank">vue3 响应性API</a> <span class="text-muted">weixin_44747590</span> <a class="tag" taget="_blank" href="/search/vue3/1.htm">vue3</a><a class="tag" taget="_blank" href="/search/vue/1.htm">vue</a><a class="tag" taget="_blank" href="/search/js/1.htm">js</a> <div>toRef可以用来为源响应式对象上的property性创建一个ref。然后可以将ref传递出去,从而保持对其源property的响应式连接。本质类似指针引用。举例:conststate=reactive({foo:1,bar:2})constfooRef=toRef(state,'foo')fooRef.value++console.log(state.foo)//2state.foo++cons</div> </li> <li><a href="/article/1834957860375719936.htm" title="手撸vue3核心源码——响应式原理(isRef和unRef)" target="_blank">手撸vue3核心源码——响应式原理(isRef和unRef)</a> <span class="text-muted">前端不是渣男</span> <a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/vue.js/1.htm">vue.js</a> <div>今天来实现一下ref的功能函数,isRef与unRefisRefisRef和isReactive一样,都是用于检测数据类型,isRef是检测是不是一个ref对象,跟isReactive函数实现起来一样,我们先来写一个单元测试这里要实现的功能是,检测ref对象肯定就通过返回true,检测普通类型数据以及reactive对象都是falseit("itshouldreturnaboolean",()=></div> </li> <li><a href="/article/1834957104239177728.htm" title="vue3 | isRef、unref、toRef、toRefs" target="_blank">vue3 | isRef、unref、toRef、toRefs</a> <span class="text-muted">杨贵妃会飞飞飞</span> <a class="tag" taget="_blank" href="/search/Vue/1.htm">Vue</a><a class="tag" taget="_blank" href="/search/vue.js/1.htm">vue.js</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a> <div>isRef检查某个值是否是ref。是返回true,否则返回false。constnum=ref(10)constnum1=20constnum2=reactive({data:30})console.log(isRef(num))//trueconsole.log(isRef(num1))//falseconsole.log(isRef(num2))//falseunref()如果参数是ref,则</div> </li> <li><a href="/article/1834953567916617728.htm" title="vue学习笔记——关于对Vue3 ref(), toRef(), toRefs(), unref(), isRef(), reactive()方法的理解。" target="_blank">vue学习笔记——关于对Vue3 ref(), toRef(), toRefs(), unref(), isRef(), reactive()方法的理解。</a> <span class="text-muted">chen_sir_sh</span> <a class="tag" taget="_blank" href="/search/vue%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/1.htm">vue学习笔记</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/vue/1.htm">vue</a> <div>VUE3出现了很多新的API,下面是自己的一些理解进行的总结。欢迎大家一起交流补充。ref()使用ref创建一个数据类型,ref有value这个属性constname1={age:"14",name:"bob1"};constname2=ref({name:"bob2"});//使用ref创建一个数据类型相对于reactive,ref有value属性name2.value="bob3"consol</div> </li> <li><a href="/article/1834951677422170112.htm" title="React项目的开发前准备 以及 JSX 的基本使用" target="_blank">React项目的开发前准备 以及 JSX 的基本使用</a> <span class="text-muted">渡鸦七</span> <a class="tag" taget="_blank" href="/search/React/1.htm">React</a><a class="tag" taget="_blank" href="/search/react.js/1.htm">react.js</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF%E6%A1%86%E6%9E%B6/1.htm">前端框架</a> <div>React项目的开发前准备以及JSX的基本使用React项目创建create-react-appnpxcreate-react-appmy-appcdmy-appnpmstartyarncreatereact-appyarncreatereact-appmy-appcdmy-appyarnstartcreate-react-app和yarncreatereact-app都可以快速创建一个React</div> </li> <li><a href="/article/1834950166029889536.htm" title="react 函数组件useState异步变同步" target="_blank">react 函数组件useState异步变同步</a> <span class="text-muted">MIKE-zi</span> <a class="tag" taget="_blank" href="/search/react%E5%85%A5%E9%97%A8/1.htm">react入门</a><a class="tag" taget="_blank" href="/search/setstate%E5%BC%82%E6%AD%A5%E9%97%AE%E9%A2%98/1.htm">setstate异步问题</a><a class="tag" taget="_blank" href="/search/%E5%87%BD%E6%95%B0%E7%BB%84%E4%BB%B6%E5%AE%9E%E7%8E%B0%E5%88%86%E9%A1%B5/1.htm">函数组件实现分页</a><a class="tag" taget="_blank" href="/search/%E5%87%BD%E6%95%B0%E7%BB%84%E4%BB%B6%E4%B8%8A%E6%8B%89%E5%8A%A0%E8%BD%BD/1.htm">函数组件上拉加载</a> <div>需求函数组件实现一个下拉上拉的列表,列表支持分页。最开始我们使用setstate的方式去报错当前页数。这样做的问题,就是有一个异步的延迟。上代码const[pageNo,setpageNo]=useState(1)constonPullUpRefresh=()=>{console.log("上拉加载内容")console.log("上一次的pageNo",pageNo)setpageNo(page</div> </li> <li><a href="/article/1834844033856401408.htm" title="Netty权威指南:Netty总结-高性能与可靠性" target="_blank">Netty权威指南:Netty总结-高性能与可靠性</a> <span class="text-muted">Ty_1106</span> <a class="tag" taget="_blank" href="/search/Netty/1.htm">Netty</a><a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E7%BD%91%E7%BB%9C/1.htm">网络</a><a class="tag" taget="_blank" href="/search/rpc/1.htm">rpc</a> <div>第二十二章高性能之道22.1RPC调用性能模型分析22.1.1传统RPC调用性能差三宗罪:网络传输采用同步阻塞I/O导致经常性阻塞序列化性能差线程模型问题22.1.2I/O通信性能三要素传输:BIO、NIO或者AIO协议:HTTP公有协议,内部私有协议线程:数据报如何读取,Reactor线程模型22.2Netty高性能之道22.2.1异步非阻塞通信I/O多路复用技术22.2.2高效的Reactor</div> </li> <li><a href="/article/1834835713846243328.htm" title="Vue 和 React 的对比" target="_blank">Vue 和 React 的对比</a> <span class="text-muted">淘淘是只狗</span> <a class="tag" taget="_blank" href="/search/vue/1.htm">vue</a><a class="tag" taget="_blank" href="/search/React/1.htm">React</a><a class="tag" taget="_blank" href="/search/vue.js/1.htm">vue.js</a><a class="tag" taget="_blank" href="/search/react.js/1.htm">react.js</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a> <div>React和Vue有许多相似之处:使用VirtualDOM提供了响应式(Reactive)和组件化(Composable)的视图组件。将注意力集中保持在核心库,而将其他功能如路由和全局状态管理交给相关的库。运行时性能React在React应用中,当某个组件的状态发生变化时,它会以该组件为根,重新渲染整个组件子树。如要避免不必要的子组件的重渲染,你需要在所有可能的地方使用PureComponent,</div> </li> <li><a href="/article/1834832435699871744.htm" title="一文让你彻底弄懂Redux的基本原理以及其如何在React中使用!" target="_blank">一文让你彻底弄懂Redux的基本原理以及其如何在React中使用!</a> <span class="text-muted">tabzzz</span> <a class="tag" taget="_blank" href="/search/react.js/1.htm">react.js</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/ecmascript/1.htm">ecmascript</a> <div>文章目录什么是Redux?它有什么用Redux基本原理Redux在React中具体使用的方法ReduxToolkit(RTK)createSlice函数参数返回值示例configureStore函数参数返回值示例React-ReduxProvider组件示例React组件使用store中的数据useSelector钩子函数示例connect组件mapStateToPropsmapDispatchT</div> </li> <li><a href="/article/1834831738988228608.htm" title="react 更新元素状态" target="_blank">react 更新元素状态</a> <span class="text-muted">叶绿素yls</span> <div>所有的react元素都是immutable不可变的。当元素被创建之后,我们无法修改他的内容或属性。根据我们现在所学的react的知识,我们要更新元素的内容,我们必须重新渲染这个元素,也就是重新创建这个元素。看一个例子:functiontick(){constelement=Hello,worldItis{newDate().toLocaleString()}.;ReactDOM.render(el</div> </li> <li><a href="/article/1834817300432252928.htm" title="Reactive 编程-Vert.x" target="_blank">Reactive 编程-Vert.x</a> <span class="text-muted">Flying_Fish_Xuan</span> <a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a> <div>Reactive编程与Vert.x:高效异步Java微服务框架一、什么是Reactive编程?Reactive编程是一种异步编程范式,专注于数据流和事件的传播处理。与传统的阻塞式编程不同,Reactive编程能够更好地处理高并发和异步操作,特别适合实时系统、流处理以及需要快速响应的场景。Reactive编程的核心原则包括:响应性(Responsive):系统能够快速响应用户请求,并保持低延迟。弹性</div> </li> <li><a href="/article/1834793357197537280.htm" title="生成一个完整的App代码通常不会仅仅通过单一的文件或几种语言的简单组合来完成,因为App的开发涉及前端用户界面、后端逻辑处理、数据库管理以及可能的第三方服务集成等多个方面。不过,我可以为你提供一个概念" target="_blank">生成一个完整的App代码通常不会仅仅通过单一的文件或几种语言的简单组合来完成,因为App的开发涉及前端用户界面、后端逻辑处理、数据库管理以及可能的第三方服务集成等多个方面。不过,我可以为你提供一个概念</a> <span class="text-muted">NewmanEdwarda2</span> <a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/ui/1.htm">ui</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a> <div>前端(用户界面)yinanjinying.comHTML/CSS/JavaScript(原生Web开发)对于简单的WebApp,你可以直接使用HTML来构建页面结构,CSS来设置样式,JavaScript来添加交互性。React(JavaScript/TypeScript)对于更复杂的单页应用(SPA),React是一个流行的选择。它允许你构建可复用的UI组件。Flutter(Dart)如果你想要</div> </li> <li><a href="/article/1834722761373741056.htm" title="react native ScrollView实现滑动锚定,滑动到指定位置" target="_blank">react native ScrollView实现滑动锚定,滑动到指定位置</a> <span class="text-muted">君君yui</span> <a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/react/1.htm">react</a><a class="tag" taget="_blank" href="/search/native/1.htm">native</a><a class="tag" taget="_blank" href="/search/react.js/1.htm">react.js</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a> <div>实现ScrollView滑动视图组件滑动到指定位置,实现tab与具体位置相锚定给需要锚定的组件加上onLayout属性//event.nativeEvent.layout.x是水平方向值,event.nativeEvent.layout.y是数值方向值//this.layoutList用于存储组件位置onLayout={(event)=>{this.layoutList.push(event.na</div> </li> <li><a href="/article/1834722508675313664.htm" title="reactnative 获取定位_React-native实现定位的功能" target="_blank">reactnative 获取定位_React-native实现定位的功能</a> <span class="text-muted">weixin_39644915</span> <a class="tag" taget="_blank" href="/search/reactnative/1.htm">reactnative</a><a class="tag" taget="_blank" href="/search/%E8%8E%B7%E5%8F%96%E5%AE%9A%E4%BD%8D/1.htm">获取定位</a> <div>1、在React-native中实现定位的功能的几种方式。(1).使用rn中的自带的Geolocation实现定位。(2).用第三放库react-native-location实现定位。2.用rn中自带的Geolocation实现定位的详细步骤:(1).如果是android进行定位手下是需要权限的通过以下代码设置权限:(2).直接通过navigator.geolocation来进行定位,示例代码如</div> </li> <li><a href="/article/1834711914299617280.htm" title="React Native动画的锚点anchorPoint" target="_blank">React Native动画的锚点anchorPoint</a> <span class="text-muted">沉默的依恋</span> <a class="tag" taget="_blank" href="/search/React/1.htm">React</a><a class="tag" taget="_blank" href="/search/Native/1.htm">Native</a> <div>在RN动画开发的过程中,有需求让图片绕中心点以外的其它点旋转,本以为是一个简单的问题,猜想Facebook应该有提供类似的API.然而在官网找了一圈没有anchorPoint这个API,后来想了想,RN与H5非常像,应该有transformOrigin这个属性,然而,还是没有,在github的issues中有人提问过什么时候更新这个属性,但是官网仍旧没有计划;为了达到这种效果,其实有一种间接的方法</div> </li> <li><a href="/article/1834705361693667328.htm" title="基于react native的锚点" target="_blank">基于react native的锚点</a> <span class="text-muted">miao_zz</span> <a class="tag" taget="_blank" href="/search/React/1.htm">React</a><a class="tag" taget="_blank" href="/search/react-native/1.htm">react-native</a><a class="tag" taget="_blank" href="/search/react/1.htm">react</a><a class="tag" taget="_blank" href="/search/native/1.htm">native</a><a class="tag" taget="_blank" href="/search/android/1.htm">android</a><a class="tag" taget="_blank" href="/search/react.js/1.htm">react.js</a> <div>基于reactnative的锚点效果示例图示例代码效果示例图示例代码/*eslint-disablereact-native/no-inline-styles*/importReact,{useEffect,useRef,useState}from'react';import{Image,ImageBackground,ScrollView,StyleSheet,Text,TouchableOpa</div> </li> <li><a href="/article/1834673478599536640.htm" title="vue2与vue3的区别" target="_blank">vue2与vue3的区别</a> <span class="text-muted">longfan_</span> <a class="tag" taget="_blank" href="/search/vue/1.htm">vue</a> <div>1.vue2和vue3响应式原理发生了改变vue2的响应式原理是利⽤es5的⼀个API,Object.defineProperty()对数据进⾏劫持结合发布订阅模式的⽅式来实现的。vue3中使⽤了es6的proxyAPI对数据代理,通过reactive()函数给每⼀个对象都包⼀层Proxy,通过Proxy监听属性的变化,从⽽实现对数据的监控。这⾥是引相⽐于vue2版本,使⽤proxy的优势如下1.</div> </li> <li><a href="/article/1834617504568274944.htm" title="前后端分离,Asp.net core webapi 如何配置跨域" target="_blank">前后端分离,Asp.net core webapi 如何配置跨域</a> <span class="text-muted">代码掌控者</span> <a class="tag" taget="_blank" href="/search/C%23/1.htm">C#</a><a class="tag" taget="_blank" href="/search/asp.net/1.htm">asp.net</a><a class="tag" taget="_blank" href="/search/core/1.htm">core</a><a class="tag" taget="_blank" href="/search/.net/1.htm">.net</a><a class="tag" taget="_blank" href="/search/core/1.htm">core</a><a class="tag" taget="_blank" href="/search/%E7%BB%8F%E9%AA%8C%E5%88%86%E4%BA%AB/1.htm">经验分享</a><a class="tag" taget="_blank" href="/search/c%23/1.htm">c#</a> <div>前言可以说,前后端分离已经成为当今信息系统项目开发的主流软件架构模式,微服务的出现,让前后端分离发展更是迅速,大量优秀的前端框架如vue.js、react的出现,也让前后端分离趋势加快。所谓的前后端分离软件架构模式,就是指将前端和后端的开发完全分离,后端负责提供API接口和数据处理,而前端通过各种现代的JavaScript技术如AJAX或者Fetch等,来调用后端提供的API接口获取数据,从而构建</div> </li> <li><a href="/article/57.htm" title="多线程编程之join()方法" target="_blank">多线程编程之join()方法</a> <span class="text-muted">周凡杨</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/JOIN/1.htm">JOIN</a><a class="tag" taget="_blank" href="/search/%E5%A4%9A%E7%BA%BF%E7%A8%8B/1.htm">多线程</a><a class="tag" taget="_blank" href="/search/%E7%BC%96%E7%A8%8B/1.htm">编程</a><a class="tag" taget="_blank" href="/search/%E7%BA%BF%E7%A8%8B/1.htm">线程</a> <div>现实生活中,有些工作是需要团队中成员依次完成的,这就涉及到了一个顺序问题。现在有T1、T2、T3三个工人,如何保证T2在T1执行完后执行,T3在T2执行完后执行?问题分析:首先问题中有三个实体,T1、T2、T3, 因为是多线程编程,所以都要设计成线程类。关键是怎么保证线程能依次执行完呢?   Java实现过程如下: public class T1 implements Runnabl</div> </li> <li><a href="/article/184.htm" title="java中switch的使用" target="_blank">java中switch的使用</a> <span class="text-muted">bingyingao</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/enum/1.htm">enum</a><a class="tag" taget="_blank" href="/search/break/1.htm">break</a><a class="tag" taget="_blank" href="/search/continue/1.htm">continue</a> <div>java中的switch仅支持case条件仅支持int、enum两种类型。 用enum的时候,不能直接写下列形式。 switch (timeType) { case ProdtransTimeTypeEnum.DAILY: break; default: br</div> </li> <li><a href="/article/311.htm" title="hive having count 不能去重" target="_blank">hive having count 不能去重</a> <span class="text-muted">daizj</span> <a class="tag" taget="_blank" href="/search/hive/1.htm">hive</a><a class="tag" taget="_blank" href="/search/%E5%8E%BB%E9%87%8D/1.htm">去重</a><a class="tag" taget="_blank" href="/search/having+count/1.htm">having count</a><a class="tag" taget="_blank" href="/search/%E8%AE%A1%E6%95%B0/1.htm">计数</a> <div>hive在使用having count()是,不支持去重计数   hive (default)> select imei from t_test_phonenum where ds=20150701 group by imei having count(distinct phone_num)>1 limit 10;  FAILED: SemanticExcep</div> </li> <li><a href="/article/438.htm" title="WebSphere对JSP的缓存" target="_blank">WebSphere对JSP的缓存</a> <span class="text-muted">周凡杨</span> <a class="tag" taget="_blank" href="/search/WAS+JSP+%E7%BC%93%E5%AD%98/1.htm">WAS JSP 缓存</a> <div>      对于线网上的工程,更新JSP到WebSphere后,有时会出现修改的jsp没有起作用,特别是改变了某jsp的样式后,在页面中没看到效果,这主要就是由于websphere中缓存的缘故,这就要清除WebSphere中jsp缓存。要清除WebSphere中JSP的缓存,就要找到WAS安装后的根目录。        现服务</div> </li> <li><a href="/article/565.htm" title="设计模式总结" target="_blank">设计模式总结</a> <span class="text-muted">朱辉辉33</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/1.htm">设计模式</a> <div>1.工厂模式   1.1 工厂方法模式 (由一个工厂类管理构造方法)      1.1.1普通工厂模式(一个工厂类中只有一个方法)      1.1.2多工厂模式(一个工厂类中有多个方法)      1.1.3静态工厂模式(将工厂类中的方法变成静态方法) &n</div> </li> <li><a href="/article/692.htm" title="实例:供应商管理报表需求调研报告" target="_blank">实例:供应商管理报表需求调研报告</a> <span class="text-muted">老A不折腾</span> <a class="tag" taget="_blank" href="/search/finereport/1.htm">finereport</a><a class="tag" taget="_blank" href="/search/%E6%8A%A5%E8%A1%A8%E7%B3%BB%E7%BB%9F/1.htm">报表系统</a><a class="tag" taget="_blank" href="/search/%E6%8A%A5%E8%A1%A8%E8%BD%AF%E4%BB%B6/1.htm">报表软件</a><a class="tag" taget="_blank" href="/search/%E4%BF%A1%E6%81%AF%E5%8C%96%E9%80%89%E5%9E%8B/1.htm">信息化选型</a> <div>引言 随着企业集团的生产规模扩张,为支撑全球供应链管理,对于供应商的管理和采购过程的监控已经不局限于简单的交付以及价格的管理,目前采购及供应商管理各个环节的操作分别在不同的系统下进行,而各个数据源都独立存在,无法提供统一的数据支持;因此,为了实现对于数据分析以提供采购决策,建立报表体系成为必须。 业务目标 1、通过报表为采购决策提供数据分析与支撑 2、对供应商进行综合评估以及管理,合理管理和</div> </li> <li><a href="/article/819.htm" title="mysql" target="_blank">mysql</a> <span class="text-muted">林鹤霄</span> <div>转载源:http://blog.sina.com.cn/s/blog_4f925fc30100rx5l.html mysql -uroot -p ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)   [root@centos var]# service mysql</div> </li> <li><a href="/article/946.htm" title="Linux下多线程堆栈查看工具(pstree、ps、pstack)" target="_blank">Linux下多线程堆栈查看工具(pstree、ps、pstack)</a> <span class="text-muted">aigo</span> <a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a> <div>原文:http://blog.csdn.net/yfkiss/article/details/6729364   1. pstree pstree以树结构显示进程$ pstree -p work | grep adsshd(22669)---bash(22670)---ad_preprocess(4551)-+-{ad_preprocess}(4552)  &n</div> </li> <li><a href="/article/1073.htm" title="html input与textarea 值改变事件" target="_blank">html input与textarea 值改变事件</a> <span class="text-muted">alxw4616</span> <a class="tag" taget="_blank" href="/search/JavaScript/1.htm">JavaScript</a> <div>// 文本输入框(input) 文本域(textarea)值改变事件 // onpropertychange(IE) oninput(w3c) $('input,textarea').on('propertychange input', function(event) {      console.log($(this).val()) });   </div> </li> <li><a href="/article/1200.htm" title="String类的基本用法" target="_blank">String类的基本用法</a> <span class="text-muted">百合不是茶</span> <a class="tag" taget="_blank" href="/search/String/1.htm">String</a> <div>  字符串的用法;     // 根据字节数组创建字符串 byte[] by = { 'a', 'b', 'c', 'd' }; String newByteString = new String(by);         1,length()  获取字符串的长度     &nbs</div> </li> <li><a href="/article/1327.htm" title="JDK1.5 Semaphore实例" target="_blank">JDK1.5 Semaphore实例</a> <span class="text-muted">bijian1013</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/thread/1.htm">thread</a><a class="tag" taget="_blank" href="/search/java%E5%A4%9A%E7%BA%BF%E7%A8%8B/1.htm">java多线程</a><a class="tag" taget="_blank" href="/search/Semaphore/1.htm">Semaphore</a> <div>Semaphore类        一个计数信号量。从概念上讲,信号量维护了一个许可集合。如有必要,在许可可用前会阻塞每一个 acquire(),然后再获取该许可。每个 release() 添加一个许可,从而可能释放一个正在阻塞的获取者。但是,不使用实际的许可对象,Semaphore 只对可用许可的号码进行计数,并采取相应的行动。 S</div> </li> <li><a href="/article/1454.htm" title="使用GZip来压缩传输量" target="_blank">使用GZip来压缩传输量</a> <span class="text-muted">bijian1013</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/GZip/1.htm">GZip</a> <div>        启动GZip压缩要用到一个开源的Filter:PJL Compressing Filter。这个Filter自1.5.0开始该工程开始构建于JDK5.0,因此在JDK1.4环境下只能使用1.4.6。         PJL Compressi</div> </li> <li><a href="/article/1581.htm" title="【Java范型三】Java范型详解之范型类型通配符" target="_blank">【Java范型三】Java范型详解之范型类型通配符</a> <span class="text-muted">bit1129</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a> <div>    定义如下一个简单的范型类,   package com.tom.lang.generics; public class Generics<T> { private T value; public Generics(T value) { this.value = value; } } </div> </li> <li><a href="/article/1708.htm" title="【Hadoop十二】HDFS常用命令" target="_blank">【Hadoop十二】HDFS常用命令</a> <span class="text-muted">bit1129</span> <a class="tag" taget="_blank" href="/search/hadoop/1.htm">hadoop</a> <div>1. 修改日志文件查看器   hdfs oev -i edits_0000000000000000081-0000000000000000089 -o edits.xml cat edits.xml   修改日志文件转储为xml格式的edits.xml文件,其中每条RECORD就是一个操作事务日志   2. fsimage查看HDFS中的块信息等 &nb</div> </li> <li><a href="/article/1835.htm" title="怎样区别nginx中rewrite时break和last" target="_blank">怎样区别nginx中rewrite时break和last</a> <span class="text-muted">ronin47</span> <div>在使用nginx配置rewrite中经常会遇到有的地方用last并不能工作,换成break就可以,其中的原理是对于根目录的理解有所区别,按我的测试结果大致是这样的。 location /    {         proxy_pass http://test; </div> </li> <li><a href="/article/1962.htm" title="java-21.中兴面试题 输入两个整数 n 和 m ,从数列 1 , 2 , 3.......n 中随意取几个数 , 使其和等于 m" target="_blank">java-21.中兴面试题 输入两个整数 n 和 m ,从数列 1 , 2 , 3.......n 中随意取几个数 , 使其和等于 m</a> <span class="text-muted">bylijinnan</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a> <div> import java.util.ArrayList; import java.util.List; import java.util.Stack; public class CombinationToSum { /* 第21 题 2010 年中兴面试题 编程求解: 输入两个整数 n 和 m ,从数列 1 , 2 , 3.......n 中随意取几个数 , 使其和等</div> </li> <li><a href="/article/2089.htm" title="eclipse svn 帐号密码修改问题" target="_blank">eclipse svn 帐号密码修改问题</a> <span class="text-muted">开窍的石头</span> <a class="tag" taget="_blank" href="/search/eclipse/1.htm">eclipse</a><a class="tag" taget="_blank" href="/search/SVN/1.htm">SVN</a><a class="tag" taget="_blank" href="/search/svn%E5%B8%90%E5%8F%B7%E5%AF%86%E7%A0%81%E4%BF%AE%E6%94%B9/1.htm">svn帐号密码修改</a> <div>问题描述:      Eclipse的SVN插件Subclipse做得很好,在svn操作方面提供了很强大丰富的功能。但到目前为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,而且一旦用户的帐号、密码保存之后,就无法再变更了。 解决思路:      删除subclipse记录的帐号、密码信息,重新输入</div> </li> <li><a href="/article/2216.htm" title="[电子商务]传统商务活动与互联网的结合" target="_blank">[电子商务]传统商务活动与互联网的结合</a> <span class="text-muted">comsci</span> <a class="tag" taget="_blank" href="/search/%E7%94%B5%E5%AD%90%E5%95%86%E5%8A%A1/1.htm">电子商务</a> <div>       某一个传统名牌产品,过去销售的地点就在某些特定的地区和阶层,现在进入互联网之后,用户的数量群突然扩大了无数倍,但是,这种产品潜在的劣势也被放大了无数倍,这种销售利润与经营风险同步放大的效应,在最近几年将会频繁出现。。。。        如何避免销售量和利润率增加的</div> </li> <li><a href="/article/2343.htm" title="java 解析 properties-使用 Properties-可以指定配置文件路径" target="_blank">java 解析 properties-使用 Properties-可以指定配置文件路径</a> <span class="text-muted">cuityang</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/properties/1.htm">properties</a> <div>#mq xdr.mq.url=tcp://192.168.100.15:61618; import java.io.IOException; import java.util.Properties; public class Test { String conf = "log4j.properties"; private static final</div> </li> <li><a href="/article/2470.htm" title="Java核心问题集锦" target="_blank">Java核心问题集锦</a> <span class="text-muted">darrenzhu</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E5%9F%BA%E7%A1%80/1.htm">基础</a><a class="tag" taget="_blank" href="/search/%E6%A0%B8%E5%BF%83/1.htm">核心</a><a class="tag" taget="_blank" href="/search/%E9%9A%BE%E7%82%B9/1.htm">难点</a> <div>注意,这里的参考文章基本来自Effective Java和jdk源码 1)ConcurrentModificationException 当你用for each遍历一个list时,如果你在循环主体代码中修改list中的元素,将会得到这个Exception,解决的办法是: 1)用listIterator, 它支持在遍历的过程中修改元素, 2)不用listIterator, new一个</div> </li> <li><a href="/article/2724.htm" title="1分钟学会Markdown语法" target="_blank">1分钟学会Markdown语法</a> <span class="text-muted">dcj3sjt126com</span> <a class="tag" taget="_blank" href="/search/markdown/1.htm">markdown</a> <div>markdown 简明语法 基本符号 *,-,+ 3个符号效果都一样,这3个符号被称为 Markdown符号 空白行表示另起一个段落 `是表示inline代码,tab是用来标记 代码段,分别对应html的code,pre标签 换行 单一段落( <p>) 用一个空白行 连续两个空格 会变成一个 <br> 连续3个符号,然后是空行</div> </li> <li><a href="/article/2851.htm" title="Gson使用二(GsonBuilder)" target="_blank">Gson使用二(GsonBuilder)</a> <span class="text-muted">eksliang</span> <a class="tag" taget="_blank" href="/search/json/1.htm">json</a><a class="tag" taget="_blank" href="/search/gson/1.htm">gson</a><a class="tag" taget="_blank" href="/search/GsonBuilder/1.htm">GsonBuilder</a> <div>转载请出自出处:http://eksliang.iteye.com/blog/2175473 一.概述     GsonBuilder用来定制java跟json之间的转换格式   二.基本使用 实体测试类: 温馨提示:默认情况下@Expose注解是不起作用的,除非你用GsonBuilder创建Gson的时候调用了GsonBuilder.excludeField</div> </li> <li><a href="/article/2978.htm" title="报ClassNotFoundException: Didn't find class "...Activity" on path: DexPathList" target="_blank">报ClassNotFoundException: Didn't find class "...Activity" on path: DexPathList</a> <span class="text-muted">gundumw100</span> <a class="tag" taget="_blank" href="/search/android/1.htm">android</a> <div>有一个工程,本来运行是正常的,我想把它移植到另一台PC上,结果报: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mobovip.bgr/com.mobovip.bgr.MainActivity}: java.lang.ClassNotFoundException: Didn't f</div> </li> <li><a href="/article/3105.htm" title="JavaWeb之JSP指令" target="_blank">JavaWeb之JSP指令</a> <span class="text-muted">ihuning</span> <a class="tag" taget="_blank" href="/search/javaweb/1.htm">javaweb</a> <div>  要点   JSP指令简介  page指令  include指令    JSP指令简介    JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分。 JSP指令的基本语法格式: <%@ 指令 属性名="</div> </li> <li><a href="/article/3232.htm" title="mac上编译FFmpeg跑ios" target="_blank">mac上编译FFmpeg跑ios</a> <span class="text-muted">啸笑天</span> <a class="tag" taget="_blank" href="/search/ffmpeg/1.htm">ffmpeg</a> <div>1、下载文件:https://github.com/libav/gas-preprocessor, 复制gas-preprocessor.pl到/usr/local/bin/下, 修改文件权限:chmod 777 /usr/local/bin/gas-preprocessor.pl 2、安装yasm-1.2.0 curl http://www.tortall.net/projects/yasm</div> </li> <li><a href="/article/3359.htm" title="sql mysql oracle中字符串连接" target="_blank">sql mysql oracle中字符串连接</a> <span class="text-muted">macroli</span> <a class="tag" taget="_blank" href="/search/oracle/1.htm">oracle</a><a class="tag" taget="_blank" href="/search/sql/1.htm">sql</a><a class="tag" taget="_blank" href="/search/mysql/1.htm">mysql</a><a class="tag" taget="_blank" href="/search/SQL+Server/1.htm">SQL Server</a> <div>有的时候,我们有需要将由不同栏位获得的资料串连在一起。每一种资料库都有提供方法来达到这个目的: MySQL: CONCAT() Oracle: CONCAT(), || SQL Server: + CONCAT() 的语法如下: Mysql 中 CONCAT(字串1, 字串2, 字串3, ...): 将字串1、字串2、字串3,等字串连在一起。 请注意,Oracle的CON</div> </li> <li><a href="/article/3486.htm" title="Git fatal: unab SSL certificate problem: unable to get local issuer ce rtificate" target="_blank">Git fatal: unab SSL certificate problem: unable to get local issuer ce rtificate</a> <span class="text-muted">qiaolevip</span> <a class="tag" taget="_blank" href="/search/%E5%AD%A6%E4%B9%A0%E6%B0%B8%E6%97%A0%E6%AD%A2%E5%A2%83/1.htm">学习永无止境</a><a class="tag" taget="_blank" href="/search/%E6%AF%8F%E5%A4%A9%E8%BF%9B%E6%AD%A5%E4%B8%80%E7%82%B9%E7%82%B9/1.htm">每天进步一点点</a><a class="tag" taget="_blank" href="/search/git/1.htm">git</a><a class="tag" taget="_blank" href="/search/%E7%BA%B5%E8%A7%82%E5%8D%83%E8%B1%A1/1.htm">纵观千象</a> <div>// 报错如下: $ git pull origin master fatal: unable to access 'https://git.xxx.com/': SSL certificate problem: unable to get local issuer ce rtificate   // 原因: 由于git最新版默认使用ssl安全验证,但是我们是使用的git未设</div> </li> <li><a href="/article/3613.htm" title="windows命令行设置wifi" target="_blank">windows命令行设置wifi</a> <span class="text-muted">surfingll</span> <a class="tag" taget="_blank" href="/search/windows/1.htm">windows</a><a class="tag" taget="_blank" href="/search/wifi/1.htm">wifi</a><a class="tag" taget="_blank" href="/search/%E7%AC%94%E8%AE%B0%E6%9C%ACwifi/1.htm">笔记本wifi</a> <div>还没有讨厌无线wifi的无尽广告么,还在耐心等待它慢慢启动么 教你命令行设置 笔记本电脑wifi: 1、开启wifi命令 netsh wlan set hostednetwork mode=allow ssid=surf8 key=bb123456 netsh wlan start hostednetwork pause 其中pause是等待输入,可以去掉 2、</div> </li> <li><a href="/article/3740.htm" title="Linux(Ubuntu)下安装sysv-rc-conf" target="_blank">Linux(Ubuntu)下安装sysv-rc-conf</a> <span class="text-muted">wmlJava</span> <a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a><a class="tag" taget="_blank" href="/search/ubuntu/1.htm">ubuntu</a><a class="tag" taget="_blank" href="/search/sysv-rc-conf/1.htm">sysv-rc-conf</a> <div>安装:sudo apt-get install sysv-rc-conf 使用:sudo sysv-rc-conf 操作界面十分简洁,你可以用鼠标点击,也可以用键盘方向键定位,用空格键选择,用Ctrl+N翻下一页,用Ctrl+P翻上一页,用Q退出。     背景知识 sysv-rc-conf是一个强大的服务管理程序,群众的意见是sysv-rc-conf比chkconf</div> </li> <li><a href="/article/3867.htm" title="svn切换环境,重发布应用多了javaee标签前缀" target="_blank">svn切换环境,重发布应用多了javaee标签前缀</a> <span class="text-muted">zengshaotao</span> <a class="tag" taget="_blank" href="/search/javaee/1.htm">javaee</a> <div>更换了开发环境,从杭州,改变到了上海。svn的地址肯定要切换的,切换之前需要将原svn自带的.svn文件信息删除,可手动删除,也可通过废弃原来的svn位置提示删除.svn时删除。   然后就是按照最新的svn地址和规范建立相关的目录信息,再将原来的纯代码信息上传到新的环境。然后再重新检出,这样每次修改后就可以看到哪些文件被修改过,这对于增量发布的规范特别有用。   检出</div> </li> </ul> </div> </div> </div> <div> <div class="container"> <div class="indexes"> <strong>按字母分类:</strong> <a href="/tags/A/1.htm" target="_blank">A</a><a href="/tags/B/1.htm" target="_blank">B</a><a href="/tags/C/1.htm" target="_blank">C</a><a href="/tags/D/1.htm" target="_blank">D</a><a href="/tags/E/1.htm" target="_blank">E</a><a href="/tags/F/1.htm" target="_blank">F</a><a href="/tags/G/1.htm" target="_blank">G</a><a href="/tags/H/1.htm" target="_blank">H</a><a href="/tags/I/1.htm" target="_blank">I</a><a href="/tags/J/1.htm" target="_blank">J</a><a href="/tags/K/1.htm" target="_blank">K</a><a href="/tags/L/1.htm" target="_blank">L</a><a href="/tags/M/1.htm" target="_blank">M</a><a href="/tags/N/1.htm" target="_blank">N</a><a href="/tags/O/1.htm" target="_blank">O</a><a href="/tags/P/1.htm" target="_blank">P</a><a href="/tags/Q/1.htm" target="_blank">Q</a><a href="/tags/R/1.htm" target="_blank">R</a><a href="/tags/S/1.htm" target="_blank">S</a><a href="/tags/T/1.htm" target="_blank">T</a><a href="/tags/U/1.htm" target="_blank">U</a><a href="/tags/V/1.htm" target="_blank">V</a><a href="/tags/W/1.htm" target="_blank">W</a><a href="/tags/X/1.htm" target="_blank">X</a><a href="/tags/Y/1.htm" target="_blank">Y</a><a href="/tags/Z/1.htm" target="_blank">Z</a><a href="/tags/0/1.htm" target="_blank">其他</a> </div> </div> </div> <footer id="footer" class="mb30 mt30"> <div class="container"> <div class="footBglm"> <a target="_blank" href="/">首页</a> - <a target="_blank" href="/custom/about.htm">关于我们</a> - <a target="_blank" href="/search/Java/1.htm">站内搜索</a> - <a target="_blank" href="/sitemap.txt">Sitemap</a> - <a target="_blank" href="/custom/delete.htm">侵权投诉</a> </div> <div class="copyright">版权所有 IT知识库 CopyRight © 2000-2050 E-COM-NET.COM , All Rights Reserved. <!-- <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">京ICP备09083238号</a><br>--> </div> </div> </footer> <!-- 代码高亮 --> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shCore.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shLegacy.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shAutoloader.js"></script> <link type="text/css" rel="stylesheet" href="/static/syntaxhighlighter/styles/shCoreDefault.css"/> <script type="text/javascript" src="/static/syntaxhighlighter/src/my_start_1.js"></script> </body> </html>