Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
598 lines (431 loc) · 41.5 KB
/
Copy pathindex.html
File metadata and controls
598 lines (431 loc) · 41.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Blog | React</title>
<meta name="viewport" content="width=device-width">
<meta property="og:title" content="Blog | React">
<meta property="og:type" content="website">
<meta property="og:url" content="https://facebook.github.io/react/blog/page6/index.html">
<meta property="og:image" content="https://facebook.github.io/react/img/logo_og.png">
<meta property="og:description" content="A JavaScript library for building user interfaces">
<meta property="fb:app_id" content="623268441017527">
<link rel="shortcut icon" href="/react/favicon.ico">
<link rel="alternate" type="application/rss+xml" title="React" href="https://facebook.github.io/react/feed.xml">
<link rel="stylesheet" href="/react/css/syntax.css">
<link rel="stylesheet" href="/react/css/codemirror.css">
<link rel="stylesheet" href="/react/css/react.css">
<script src="//use.typekit.net/vqa1hcx.js"></script>
<script>try{Typekit.load();}catch(e){}</script>
<!--[if lte IE 8]>
<script src="/react/js/html5shiv.min.js"></script>
<script src="/react/js/es5-shim.min.js"></script>
<script src="/react/js/es5-sham.min.js"></script>
<![endif]-->
<script src="/react/js/codemirror.js"></script>
<script src="/react/js/javascript.js"></script>
<script src="/react/js/react.js"></script>
<script src="/react/js/react-dom.js"></script>
<script src="/react/js/babel-browser.min.js"></script>
<script src="/react/js/live_editor.js"></script>
</head>
<body>
<div class="container">
<div class="nav-main">
<div class="wrap">
<a class="nav-home" href="/react/index.html">
<img class="nav-logo" src="/react/img/logo.svg" width="36" height="36">
React
</a>
<ul class="nav-site nav-site-internal">
<li><a href="/react/docs/getting-started.html">Docs</a></li>
<li><a href="/react/support.html">Support</a></li>
<li><a href="/react/downloads.html">Download</a></li>
<li><a href="/react/blog/" class="active">Blog</a></li>
</ul>
<ul class="nav-site nav-site-external">
<li><a href="https://github.com/facebook/react">GitHub</a></li>
<li><a href="https://facebook.github.io/react-native/">React Native</a></li>
</ul>
</div>
</div>
<section class="content wrap blogContent">
<div class="nav-docs nav-blog">
<div class="nav-docs-section">
<h3>Recent posts</h3>
<ul>
<li><a href="/react/blog/2016/02/19/new-versioning-scheme.html">New Versioning Scheme</a></li>
<li><a href="/react/blog/2016/01/12/discontinuing-ie8-support.html">Discontinuing IE 8 Support in React DOM</a></li>
<li><a href="/react/blog/2016/01/08/A-implies-B-does-not-imply-B-implies-A.html">(A => B) !=> (B => A)</a></li>
<li><a href="/react/blog/2015/12/29/react-v0.14.4.html">React v0.14.4</a></li>
<li><a href="/react/blog/2015/12/18/react-components-elements-and-instances.html">React Components, Elements, and Instances</a></li>
<li><a href="/react/blog/2015/12/16/ismounted-antipattern.html">isMounted is an Antipattern</a></li>
<li><a href="/react/blog/2015/12/04/react-js-conf-2016-diversity-scholarship.html">React.js Conf 2016 Diversity Scholarship</a></li>
<li><a href="/react/blog/2015/11/18/react-v0.14.3.html">React v0.14.3</a></li>
<li><a href="/react/blog/2015/11/02/react-v0.14.2.html">React v0.14.2</a></li>
<li><a href="/react/blog/2015/10/28/react-v0.14.1.html">React v0.14.1</a></li>
<li><a href="/react/blog/all.html">All posts ...</a></li>
</ul>
</div>
</div>
<div class="inner-content">
<div class="post-list-item">
<h1>
<a href="/react/blog/2015/04/17/react-native-v0.4.html">React Native v0.4</a>
</h1>
<p class="meta">
April 17, 2015
by
<a href="https://twitter.com/vjeux">Vjeux</a>
</p>
<hr>
<div class="post">
<p>It's been three weeks since we open sourced React Native and there's been some insane amount of activity already: over 12.5k stars, 1000 commits, 500 issues, 380 pull requests, and 100 contributors, plus <a href="http://react.parts/native-ios">35 plugins</a> and <a href="http://herman.asia/building-a-flashcard-app-with-react-native">1 app in the app store</a>! We were expecting some buzz around the project but this is way beyond anything we imagined. Thank you!</p>
<p>I'd especially like to thank community members Brent Vatne and James Ide who have both already contributed meaningfully to the project and have been extremely helpful on IRC and with issues and pull requests</p>
<h2><a class="anchor" name="changelog"></a>Changelog <a class="hash-link" href="#changelog">#</a></h2>
<p>The main focus of the past few weeks has been to make React Native the best possible experience for people outside of Facebook. Here's a high level summary of what's happened since we open sourced:</p>
<ul>
<li><strong>Error messages and documentation</strong>: We want React Native to be the absolute best developer experience for building mobile apps. We've added a lot of warnings, improved the documentation, and fixed many bugs. If you encounter anything, and I really mean anything, that is not expected or clear, please create an issue - we want to hear about it and fix it.</li>
<li><strong>NPM modules compatibility</strong>: There are a lot of libraries on NPM that do not depend on node/browser internals that would be really useful in React Native, such as superagent, underscore, parse, and many others. The packager is now a lot more faithful to node/browserify/webpack dependency resolution. If your favorite library doesn't work out of the box, please open up an issue.</li>
<li><strong>Infrastructure</strong>: We are refactoring the internals of React Native to make it easier to plug in to existing iOS codebases, as well as improve performance by removing redundant views and shadow views, supporting multiple root views and manually registering classes to reduce startup time.</li>
<li><strong>Components</strong>: The API for a lot of UI components and APIs, especially the ones we're not using heavily inside of Facebook, has dramatically improved thanks to many of your pull requests.</li>
<li><strong>Tests</strong>: We ported JavaScript tests, iOS Snapshot tests, and End to End tests to Travis CI. We have broken GitHub master a couple of times (whoops!) when syncing and we hope that with this growing suite of tests it's going to become harder and harder to do so.</li>
<li><strong>Patent Grant</strong>: Many of you had concerns and questions around the PATENTS file. We pushed <a href="https://code.facebook.com/posts/1639473982937255/updating-our-open-source-patent-grant/">a new version of the grant</a>.</li>
<li><strong>Per commit history</strong>: In order to synchronize from Facebook to GitHub, we used to do one giant commit every few days. We improved our tooling and now have per commit history that maintains author information (both internal and external from pull requests), and we retroactively applied this to historical diffs to provide proper attribution.</li>
</ul>
<h2><a class="anchor" name="where-are-we-going"></a>Where are we going? <a class="hash-link" href="#where-are-we-going">#</a></h2>
<p>In addition to supporting pull requests, issues, and general improvements, we're also working hard on our internal React Native integrations and on React Native for Android.</p>
</div>
</div>
<div class="post-list-item">
<h1>
<a href="/react/blog/2015/03/30/community-roundup-26.html">Community Round-up #26</a>
</h1>
<p class="meta">
March 30, 2015
by
<a href="https://twitter.com/vjeux">Vjeux</a>
</p>
<hr>
<div class="post">
<p>We open sourced React Native last week and the community reception blew away all our expectations! So many of you tried it, made cool stuff with it, raised many issues and even submitted pull requests to fix them! The entire team wants to say thank you!</p>
<blockquote class="twitter-tweet" lang="en"><p><a href="https://twitter.com/hashtag/reactnative?src=hash">#reactnative</a> is like when you get a new expansion pack, and everybody is running around clueless about which NPC to talk to for the quests</p>— Ryan Florence (@ryanflorence) <a href="https://twitter.com/ryanflorence/status/581810423554543616">March 28, 2015</a></blockquote>
<h2><a class="anchor" name="when-is-react-native-android-coming"></a>When is React Native Android coming? <a class="hash-link" href="#when-is-react-native-android-coming">#</a></h2>
<p><strong>Give us 6 months</strong>. At Facebook, we strive to only open-source projects that we are using in production. While the Android backend for React Native is starting to work (see video below at 37min), it hasn't been shipped to any users yet. There's a lot of work that goes into open-sourcing a project, and we want to do it right so that you have a great experience when using it.</p>
<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/X6YbAKiLCLU?start=2220" frameborder="0" allowfullscreen></iframe>
<h2><a class="anchor" name="ray-wenderlich---property-finder"></a>Ray Wenderlich - Property Finder <a class="hash-link" href="#ray-wenderlich---property-finder">#</a></h2>
<p>If you are getting started with React Native, you should absolutely <a href="http://www.raywenderlich.com/99473/introducing-react-native-building-apps-javascript">use this tutorial</a> from Colin Eberhardt. It goes through all the steps to make a reasonably complete app.</p>
<p><center>
<a href="http://www.raywenderlich.com/99473/introducing-react-native-building-apps-javascript"><img src="/react/img/blog/property-finder.png" alt=""></a>
</center></p>
<p>Colin also <a href="http://blog.scottlogic.com/2015/03/26/react-native-retrospective.html">blogged about his experience using React Native</a> for a few weeks and gives his thoughts on why you would or wouldn't use it.</p>
<h2><a class="anchor" name="the-changelog"></a>The Changelog <a class="hash-link" href="#the-changelog">#</a></h2>
<p>Spencer Ahrens and I had the great pleasure to talk about React Native on <a href="https://thechangelog.com/149/">The Changelog</a> podcast. It was really fun to chat for an hour, I hope that you'll enjoy listening to it. :)</p>
<p><audio src="http://fdlyr.co/d/changelog/cdn.5by5.tv/audio/broadcasts/changelog/2015/changelog-149.mp3" controls="controls" style="width: 100%"></audio></p>
<h2><a class="anchor" name="hacker-news"></a>Hacker News <a class="hash-link" href="#hacker-news">#</a></h2>
<p>Less than 24 hours after React Native was open sourced, Simarpreet Singh built an <a href="https://github.com/iSimar/HackerNews-React-Native">Hacker News reader app from scratch</a>. It's unbelievable how fast he was able to pull it off!</p>
<p><center>
<a href="https://github.com/iSimar/HackerNews-React-Native"><img src="/react/img/blog/hacker-news-react-native.png" alt=""></a>
</center></p>
<h2><a class="anchor" name="parse--react"></a>Parse + React <a class="hash-link" href="#parse--react">#</a></h2>
<p>There's a huge ecosystem of JavaScript modules on npm and React Native was designed to work well with the ones that don't have DOM dependencies. Parse is a great example; you can <code>npm install parse</code> on your React Native project and it'll work as is. :) We still have <a href="https://github.com/facebook/react-native/issues/406">a</a> <a href="https://github.com/facebook/react-native/issues/370">few</a> <a href="https://github.com/facebook/react-native/issues/316">issues</a> to solve; please create an issue if your favorite library doesn't work out of the box.</p>
<p><center>
<a href="http://blog.parse.com/2015/03/25/parse-and-react-shared-chemistry/"><img src="/react/img/blog/parse-react.jpg" alt=""></a>
</center></p>
<h2><a class="anchor" name="tcomb-form-native"></a>tcomb-form-native <a class="hash-link" href="#tcomb-form-native">#</a></h2>
<p>Giulio Canti is the author of the <a href="https://github.com/gcanti/tcomb-form">tcomb-form library</a> for React. He already <a href="https://github.com/gcanti/tcomb-form-native">ported it to React Native</a> and it looks great!</p>
<p><center>
<a href="https://github.com/gcanti/tcomb-form-native"><img src="/react/img/blog/tcomb-react-native.png" alt=""></a>
</center></p>
<h2><a class="anchor" name="facebook-login-with-react-native"></a>Facebook Login with React Native <a class="hash-link" href="#facebook-login-with-react-native">#</a></h2>
<p>One of the reason we built React Native is to be able to use all the libraries in the native ecosystem. Brent Vatne leads the way and explains <a href="http://brentvatne.ca/facebook-login-with-react-native/">how to use Facebook Login with React Native</a>.</p>
<h2><a class="anchor" name="modus-create"></a>Modus Create <a class="hash-link" href="#modus-create">#</a></h2>
<p>Jay Garcia spent a lot of time during the beta working on a NES music player with React Native. He wrote a blog post to share his experience and explains some code snippets.</p>
<p><center>
<a href="http://moduscreate.com/react-native-has-landed/"><img src="/react/img/blog/modus-create.gif" alt=""></a>
</center></p>
<h2><a class="anchor" name="react-native-with-babel-and-webpack"></a>React Native with Babel and Webpack <a class="hash-link" href="#react-native-with-babel-and-webpack">#</a></h2>
<p>React Native ships with a custom packager and custom ES6 transforms instead of using what the open source community settled on such as Webpack and Babel. The main reason for this is performance – we couldn't get those tools to have sub-second reload time on a large codebase.</p>
<p>Roman Liutikov found a way to <a href="https://github.com/roman01la/react-native-babel">use Webpack and Babel to run on React Native</a>! In the future, we want to work with those projects to provide cleaner extension mechanisms.</p>
<h2><a class="anchor" name="a-dynamic-crazy-native-mobile-futurepowered-by-javascript"></a>A Dynamic, Crazy, Native Mobile Future—Powered by JavaScript <a class="hash-link" href="#a-dynamic-crazy-native-mobile-futurepowered-by-javascript">#</a></h2>
<p>Clay Allsopp wrote a post about <a href="https://medium.com/@clayallsopp/a-dynamic-crazy-native-mobile-future-powered-by-javascript-70f2d56b1987">all the crazy things you could do with a JavaScript engine that renders native views</a>. What about native embeds, seamless native browser, native search engine or even app generation...</p>
<h2><a class="anchor" name="random-tweet"></a>Random Tweet <a class="hash-link" href="#random-tweet">#</a></h2>
<p>We've spent a lot of efforts getting the onboarding as easy as possible and we're really happy that people noticed. We still have a lot of work to do on documentation, stay tuned!</p>
<blockquote class="twitter-tweet" lang="en"><p>Wow. Getting started with React Native might have been the smoothest experience I’ve ever had with a new developer product.</p>— Andreas Eldh (@eldh) <a href="https://twitter.com/eldh/status/581186172094980096">March 26, 2015</a></blockquote>
</div>
</div>
<div class="post-list-item">
<h1>
<a href="/react/blog/2015/03/26/introducing-react-native.html">Introducing React Native</a>
</h1>
<p class="meta">
March 26, 2015
by
<a href="http://benalpert.com">Ben Alpert</a>
</p>
<hr>
<div class="post">
<p>In January at React.js Conf, we announced React Native, a new framework for building native apps using React. We're happy to announce that we're open-sourcing React Native and you can start building your apps with it today.</p>
<p>For more details, see <a href="https://code.facebook.com/posts/1014532261909640/react-native-bringing-modern-web-techniques-to-mobile/">Tom Occhino's post on the Facebook Engineering blog</a>:</p>
<blockquote>
<p><em>What we really want is the user experience of the native mobile platforms, combined with the developer experience we have when building with React on the web.</em></p>
<p><em>With a bit of work, we can make it so the exact same React that's on GitHub can power truly native mobile applications. The only difference in the mobile environment is that instead of running React in the browser and rendering to divs and spans, we run it an embedded instance of JavaScriptCore inside our apps and render to higher-level platform-specific components.</em></p>
<p><em>It's worth noting that we're not chasing “write once, run anywhere.” Different platforms have different looks, feels, and capabilities, and as such, we should still be developing discrete apps for each platform, but the same set of engineers should be able to build applications for whatever platform they choose, without needing to learn a fundamentally different set of technologies for each. We call this approach “learn once, write anywhere.”</em></p>
</blockquote>
<p>To learn more, visit the <a href="https://facebook.github.io/react-native/">React Native website</a>.</p>
</div>
</div>
<div class="post-list-item">
<h1>
<a href="/react/blog/2015/03/19/building-the-facebook-news-feed-with-relay.html">Building The Facebook News Feed With Relay</a>
</h1>
<p class="meta">
March 19, 2015
by
<a href="https://twitter.com/en_JS">Joseph Savona</a>
</p>
<hr>
<div class="post">
<p>At React.js Conf in January we gave a preview of Relay, a new framework for building data-driven applications in React. In this post, we'll describe the process of creating a Relay application. This post assumes some familiarity with the concepts of Relay and GraphQL, so if you haven't already we recommend reading <a href="/react/blog/2015/02/20/introducing-relay-and-graphql.html">our introductory blog post</a> or watching <a href="https://www.youtube-nocookie.com/v/9sc8Pyc51uU">the conference talk</a>.</p>
<p>We're working hard to prepare GraphQL and Relay for public release. In the meantime, we'll continue to provide information about what you can expect.</p>
<p><br/></p>
<h2><a class="anchor" name="the-relay-architecture"></a>The Relay Architecture <a class="hash-link" href="#the-relay-architecture">#</a></h2>
<p>The diagram below shows the main parts of the Relay architecture on the client and the server:</p>
<p><img src="/react/img/blog/relay-components/relay-architecture.png" alt="Relay Architecture" width="650" /></p>
<p>The main pieces are as follows:</p>
<ul>
<li>Relay Components: React components annotated with declarative data descriptions.</li>
<li>Actions: Descriptions of how data should change in response to user actions.</li>
<li>Relay Store: A client-side data store that is fully managed by the framework.</li>
<li>Server: An HTTP server with GraphQL endpoints (one for reads, one for writes) that respond to GraphQL queries.</li>
</ul>
<p>This post will focus on <strong>Relay components</strong> that describe encapsulated units of UI and their data dependencies. These components form the majority of a Relay application.</p>
<p><br/></p>
<h2><a class="anchor" name="a-relay-application"></a>A Relay Application <a class="hash-link" href="#a-relay-application">#</a></h2>
<p>To see how components work and can be composed, let's implement a basic version of the Facebook News Feed in Relay. Our application will have two components: a <code><NewsFeed></code> that renders a list of <code><Story></code> items. We'll introduce the plain React version of each component first and then convert it to a Relay component. The goal is something like the following:</p>
<p><img src="/react/img/blog/relay-components/sample-newsfeed.png" alt="Sample News Feed" width="360" /></p>
<p><br/></p>
<h2><a class="anchor" name="the-ltstorygt-begins"></a>The <code><Story></code> Begins <a class="hash-link" href="#the-ltstorygt-begins">#</a></h2>
<p>The first step is a React <code><Story></code> component that accepts a <code>story</code> prop with the story's text and author information. Note that all examples uses ES6 syntax and elide presentation details to focus on the pattern of data access.</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// Story.react.js</span>
<span class="kr">export</span> <span class="k">default</span> <span class="kr">class</span> <span class="nx">Story</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">Component</span> <span class="p">{</span>
<span class="nx">render</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">story</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">story</span><span class="p">;</span>
<span class="k">return</span> <span class="p">(</span>
<span class="o"><</span><span class="nx">View</span><span class="o">></span>
<span class="o"><</span><span class="nx">Image</span> <span class="nx">uri</span><span class="o">=</span><span class="p">{</span><span class="nx">story</span><span class="p">.</span><span class="nx">author</span><span class="p">.</span><span class="nx">profilePicture</span><span class="p">.</span><span class="nx">uri</span><span class="p">}</span> <span class="o">/></span>
<span class="o"><</span><span class="nx">Text</span><span class="o">></span><span class="p">{</span><span class="nx">story</span><span class="p">.</span><span class="nx">author</span><span class="p">.</span><span class="nx">name</span><span class="p">}</span><span class="o"><</span><span class="err">/Text></span>
<span class="o"><</span><span class="nx">Text</span><span class="o">></span><span class="p">{</span><span class="nx">story</span><span class="p">.</span><span class="nx">text</span><span class="p">}</span><span class="o"><</span><span class="err">/Text></span>
<span class="o"><</span><span class="err">/View></span>
<span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p><br/></p>
<h2><a class="anchor" name="whats-the-ltstorygt"></a>What's the <code><Story></code>? <a class="hash-link" href="#whats-the-ltstorygt">#</a></h2>
<p>Relay automates the process of fetching data for components by wrapping existing React components in Relay containers (themselves React components):</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// Story.react.js</span>
<span class="kr">class</span> <span class="nx">Story</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">Component</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
<span class="kr">export</span> <span class="k">default</span> <span class="nx">Relay</span><span class="p">.</span><span class="nx">createContainer</span><span class="p">(</span><span class="nx">Story</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">fragments</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">story</span><span class="o">:</span> <span class="cm">/* TODO */</span>
<span class="p">}</span>
<span class="p">});</span>
</code></pre></div>
<p>Before adding the GraphQL fragment, let's look at the component hierarchy this creates:</p>
<p><img src="/react/img/blog/relay-components/relay-containers.png" width="397" alt="React Container Data Flow" /></p>
<p>Most props will be passed through from the container to the original component. However, Relay will return the query results for a prop whenever a fragment is defined. In this case we'll add a GraphQL fragment for <code>story</code>:</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// Story.react.js</span>
<span class="kr">class</span> <span class="nx">Story</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">Component</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
<span class="kr">export</span> <span class="k">default</span> <span class="nx">Relay</span><span class="p">.</span><span class="nx">createContainer</span><span class="p">(</span><span class="nx">Story</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">fragments</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">story</span><span class="o">:</span> <span class="p">()</span> <span class="o">=></span> <span class="nx">Relay</span><span class="p">.</span><span class="nx">QL</span><span class="err">`</span>
<span class="nx">fragment</span> <span class="nx">on</span> <span class="nx">Story</span> <span class="p">{</span>
<span class="nx">author</span> <span class="p">{</span>
<span class="nx">name</span>
<span class="nx">profilePicture</span> <span class="p">{</span>
<span class="nx">uri</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nx">text</span>
<span class="p">}</span>
<span class="err">`</span><span class="p">,</span>
<span class="p">},</span>
<span class="p">});</span>
</code></pre></div>
<p>Queries use ES6 template literals tagged with the <code>Relay.QL</code> function. Similar to how JSX transpiles to plain JavaScript objects and function calls, these template literals transpile to plain objects that describe fragments. Note that the fragment's structure closely matches the object structure that we expected in <code><Story></code>'s render function.</p>
<p><br/></p>
<h2><a class="anchor" name="ltstorygts-on-demand"></a><code><Story></code>s on Demand <a class="hash-link" href="#ltstorygts-on-demand">#</a></h2>
<p>We can render a Relay component by providing Relay with the component (<code><Story></code>) and the ID of the data (a story ID). Given this information, Relay will first fetch the results of the query and then <code>render()</code> the component. The value of <code>props.story</code> will be a plain JavaScript object such as the following:</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="p">{</span>
<span class="nx">author</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">name</span><span class="o">:</span> <span class="s2">"Greg"</span><span class="p">,</span>
<span class="nx">profilePicture</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">uri</span><span class="o">:</span> <span class="s2">"https://…"</span>
<span class="p">}</span>
<span class="p">},</span>
<span class="nx">text</span><span class="o">:</span> <span class="s2">"The first Relay blog post is up…"</span>
<span class="p">}</span>
</code></pre></div>
<p>Relay guarantees that all data required to render a component will be available before it is rendered. This means that <code><Story></code> does not need to handle a loading state; the <code>story</code> is <em>guaranteed</em> to be available before <code>render()</code> is called. We have found that this invariant simplifies our application code <em>and</em> improves the user experience. Of course, Relay also has options to delay the fetching of some parts of our queries.</p>
<p>The diagram below shows how Relay containers make data available to our React components:</p>
<p><img src="/react/img/blog/relay-components/relay-containers-data-flow.png" width="650" alt="Relay Container Data Flow" /></p>
<p><br/></p>
<h2><a class="anchor" name="ltnewsfeedgt-worthy"></a><code><NewsFeed></code> Worthy <a class="hash-link" href="#ltnewsfeedgt-worthy">#</a></h2>
<p>Now that the <code><Story></code> is over we can continue with the <code><NewsFeed></code> component. Again, we'll start with a React version:</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// NewsFeed.react.js</span>
<span class="kr">class</span> <span class="nx">NewsFeed</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">Component</span> <span class="p">{</span>
<span class="nx">render</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">stories</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">viewer</span><span class="p">.</span><span class="nx">stories</span><span class="p">;</span> <span class="c1">// `viewer` is the active user</span>
<span class="k">return</span> <span class="p">(</span>
<span class="o"><</span><span class="nx">View</span><span class="o">></span>
<span class="p">{</span><span class="nx">stories</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">story</span> <span class="o">=></span> <span class="o"><</span><span class="nx">Story</span> <span class="nx">story</span><span class="o">=</span><span class="p">{</span><span class="nx">story</span><span class="p">}</span> <span class="o">/></span><span class="p">)}</span>
<span class="o"><</span><span class="nx">Button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{()</span> <span class="o">=></span> <span class="k">this</span><span class="p">.</span><span class="nx">loadMore</span><span class="p">()}</span><span class="o">></span><span class="nx">Load</span> <span class="nx">More</span><span class="o"><</span><span class="err">/Button></span>
<span class="o"><</span><span class="err">/View></span>
<span class="p">);</span>
<span class="p">}</span>
<span class="nx">loadMore</span><span class="p">()</span> <span class="p">{</span>
<span class="c1">// TODO: fetch more stories</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">NewsFeed</span><span class="p">;</span>
</code></pre></div>
<p><br/></p>
<h2><a class="anchor" name="all-the-news-fit-to-be-relayed"></a>All the News Fit to be Relayed <a class="hash-link" href="#all-the-news-fit-to-be-relayed">#</a></h2>
<p><code><NewsFeed></code> has two new requirements: it composes <code><Story></code> and requests more data at runtime.</p>
<p>Just as React views can be nested, Relay components can compose query fragments from child components. Composition in GraphQL uses ES6 template literal substitution: <code>${Component.getFragment('prop')}</code>. Pagination can be accomplished with a variable, specified with <code>$variable</code> (as in <code>stories(first: $count)</code>):</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// NewsFeed.react.js</span>
<span class="kr">class</span> <span class="nx">NewsFeed</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">Component</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
<span class="kr">export</span> <span class="k">default</span> <span class="nx">Relay</span><span class="p">.</span><span class="nx">createContainer</span><span class="p">(</span><span class="nx">NewsFeed</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">initialVariables</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">count</span><span class="o">:</span> <span class="mi">3</span> <span class="cm">/* default to 3 stories */</span>
<span class="p">},</span>
<span class="nx">fragments</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">viewer</span><span class="o">:</span> <span class="p">()</span> <span class="o">=></span> <span class="nx">Relay</span><span class="p">.</span><span class="nx">QL</span><span class="err">`</span>
<span class="nx">fragment</span> <span class="nx">on</span> <span class="nx">Viewer</span> <span class="p">{</span>
<span class="nx">stories</span><span class="p">(</span><span class="nx">first</span><span class="o">:</span> <span class="nx">$count</span><span class="p">)</span> <span class="p">{</span> <span class="cm">/* fetch viewer's stories */</span>
<span class="nx">edges</span> <span class="p">{</span>
BD0
<span class="cm">/* traverse the graph */</span>
<span class="nx">node</span> <span class="p">{</span>
<span class="nx">$</span><span class="p">{</span><span class="nx">Story</span><span class="p">.</span><span class="nx">getFragment</span><span class="p">(</span><span class="s1">'story'</span><span class="p">)}</span> <span class="cm">/* compose child fragment */</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="err">`</span><span class="p">,</span>
<span class="p">},</span>
<span class="p">});</span>
</code></pre></div>
<p>Whenever <code><NewsFeed></code> is rendered, Relay will recursively expand all the composed fragments and fetch the queries in a single trip to the server. In this case, the <code>text</code> and <code>author</code> data will be fetched for each of the 3 story nodes.</p>
<p>Query variables are available to components as <code>props.relay.variables</code> and can be modified with <code>props.relay.setVariables(nextVariables)</code>. We can use these to implement pagination:</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// NewsFeed.react.js</span>
<span class="kr">class</span> <span class="nx">NewsFeed</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">Component</span> <span class="p">{</span>
<span class="nx">render</span><span class="p">()</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
<span class="nx">loadMore</span><span class="p">()</span> <span class="p">{</span>
<span class="c1">// read current params</span>
<span class="kd">var</span> <span class="nx">count</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">relay</span><span class="p">.</span><span class="nx">variables</span><span class="p">.</span><span class="nx">count</span><span class="p">;</span>
<span class="c1">// update params</span>
<span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">relay</span><span class="p">.</span><span class="nx">setVariables</span><span class="p">({</span>
<span class="nx">count</span><span class="o">:</span> <span class="nx">count</span> <span class="o">+</span> <span class="mi">5</span><span class="p">,</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Now when <code>loadMore()</code> is called, Relay will send a GraphQL request for the additional five stories. When these stories are fetched, the component will re-render with the new stories available in <code>props.viewer.stories</code> and the updated count reflected in <code>props.relay.variables.count</code>.</p>
<p><br/></p>
<h2><a class="anchor" name="in-conclusion"></a>In Conclusion <a class="hash-link" href="#in-conclusion">#</a></h2>
<p>These two components form a solid core for our application. With the use of Relay containers and GraphQL queries, we've enabled the following benefits:</p>
<ul>
<li>Automatic and efficient pre-fetching of data for an entire view hierarchy in a single network request.</li>
<li>Trivial pagination with automatic optimizations to fetch only the additional items.</li>
<li>View composition and reusability, so that <code><Story></code> can be used on its own or within <code><NewsFeed></code>, without any changes to either component.</li>
<li>Automatic subscriptions, so that components will re-render if their data changes. Unaffected components will not re-render unnecessarily.</li>
<li>Exactly <em>zero</em> lines of imperative data fetching logic. Relay takes full advantage of React's declarative component model.</li>
</ul>
<p>But Relay has many more tricks up its sleeve. For example, it's built from the start to handle reads and writes, allowing for features like optimistic client updates with transactional rollback. Relay can also defer fetching select parts of queries, and it uses a local data store to avoid fetching the same data twice. These are all powerful features that we hope to explore in future posts.</p>
</div>
</div>
<div class="post-list-item">
<h1>
<a href="/react/blog/2015/03/16/react-v0.13.1.html">React v0.13.1</a>
</h1>
<p class="meta">
March 16, 2015
by
<a href="https://twitter.com/zpao">Paul O’Shannessy</a>
</p>
<hr>
<div class="post">
<p>It's been less than a week since we shipped v0.13.0 but it's time to do another quick release. We just released v0.13.1 which contains bugfixes for a number of small issues.</p>
<p>Thanks all of you who have been upgrading your applications and taking the time to report issues. And a huge thank you to those of you who submitted pull requests for the issues you found! 2 of the 6 fixes that went out today came from people who aren't on the core team!</p>
<p>The release is now available for download:</p>
<ul>
<li><strong>React</strong><br>
Dev build with warnings: <a href="https://fb.me/react-0.13.1.js">https://fb.me/react-0.13.1.js</a><br>
Minified build for production: <a href="https://fb.me/react-0.13.1.min.js">https://fb.me/react-0.13.1.min.js</a><br></li>
<li><strong>React with Add-Ons</strong><br>
Dev build with warnings: <a href="https://fb.me/react-with-addons-0.13.1.js">https://fb.me/react-with-addons-0.13.1.js</a><br>
Minified build for production: <a href="https://fb.me/react-with-addons-0.13.1.min.js">https://fb.me/react-with-addons-0.13.1.min.js</a><br></li>
<li><strong>In-Browser JSX transformer</strong><br>
<a href="https://fb.me/JSXTransformer-0.13.1.js">https://fb.me/JSXTransformer-0.13.1.js</a></li>
</ul>
<p>We've also published version <code>0.13.1</code> of the <code>react</code> and <code>react-tools</code> packages on npm and the <code>react</code> package on bower.</p>
<hr>
<h2><a class="anchor" name="changelog"></a>Changelog <a class="hash-link" href="#changelog">#</a></h2><h3><a class="anchor" name="react-core"></a>React Core <a class="hash-link" href="#react-core">#</a></h3><h4><a class="anchor" name="bug-fixes"></a>Bug Fixes <a class="hash-link" href="#bug-fixes">#</a></h4>
<ul>
<li>Don't throw when rendering empty <code><select></code> elements</li>
<li>Ensure updating <code>style</code> works when transitioning from <code>null</code></li>
</ul>
<h3><a class="anchor" name="react-with-add-ons"></a>React with Add-Ons <a class="hash-link" href="#react-with-add-ons">#</a></h3><h3><a class="anchor" name="bug-fixes"></a>Bug Fixes <a class="hash-link" href="#bug-fixes">#</a></h3>
<ul>
<li>TestUtils: Don't warn about <code>getDOMNode</code> for ES6 classes</li>
<li>TestUtils: Ensure wrapped full page components (<code><html></code>, <code><head></code>, <code><body></code>) are treated as DOM components</li>
<li>Perf: Stop double-counting DOM components</li>
</ul>
<h3><a class="anchor" name="react-tools"></a>React Tools <a class="hash-link" href="#react-tools">#</a></h3><h4><a class="anchor" name="bug-fixes"></a>Bug Fixes <a class="hash-link" href="#bug-fixes">#</a></h4>
<ul>
<li>Fix option parsing for <code>--non-strict-es6module</code></li>
</ul>
</div>
</div>
<div class="pagination">
<a href="/react/blog/page5/" class="previous">
« Previous Page
</a>
<a href="/react/blog/page7/" class="next">
Next Page »
</a>
</div>
</div>
</section>
<footer class="wrap">
<div class="left">
A Facebook & Instagram collaboration.<br>
<a href="/react/acknowledgements.html">Acknowledgements</a>
</div>
<div class="right">
© 2013–2016 Facebook Inc.<br>
Documentation licensed under <a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>.
</div>
</footer>
</div>
<div id="fb-root"></div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-41298772-1', 'facebook.github.io');
ga('send', 'pageview');
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=623268441017527";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
</body>
</html>