The library for web and native user interfaces.
Break lines
petehunt committed
Jun 3, 2013 at 11:19 UTC
0244123a5233bfa5b017e060caa8db77226b0745
1 file changed
+98
-34
docs/_posts/2013-06-03-why-react.md
+98
-34
@@ -4,57 +4,121 @@ layout: post
4
author: petehunt
5
---
6
7
-There are a lot of JavaScript MVC frameworks out there. Why did we build React, and why would you want to use it?
7
+There are a lot of JavaScript MVC frameworks out there. Why did we
8
+build React, and why would you want to use it?
9
10
## React is not an MVC framework.
11
11
-It's a library designed for building big UIs. The type where you have lots of reusable components that are handling events, presenting backend data, and accepting user input. The type where you have to integrate with legacy code, and support legacy browsers.
12
+It's a library designed for building big UIs. The type where you have
13
+lots of reusable components that are handling events, presenting
14
+backend data, and accepting user input. The type where you have to
15
+integrate with legacy code, and support legacy browsers.
16
13
-In a conventional MVC application, you'd build the View with React (and maybe the Controller too, if you'd like).
17
+In a conventional MVC application, you'd build the View with React
18
+(and maybe the Controller too, if you'd like).
19
20
## React doesn't use templates.
21
17
-Traditionally, you'd create a set of templates with a template language or HTML directives to make a page dynamic. It's up to the designer of the template language or the author of the directives to provide the full set of abstractions you can use to build your front-end code.
18
-
19
-React's technique is to break your view down into small, composable and reusable **components**. These components provide a `render()` method which specifies how the component will generate its markup. `render()` can either return normal DOM elements (like `<div>`s) or can return other components.
20
-
21
-This means that yes, we have JavaScript generating markup. But we think that this is an advantage over using templates for a few reasons:
22
-
23
-- **JavaScript is a flexible, powerful programming language with the ability to build abstractions.** This is incredibly important in large applications.
24
-- "Logic" and "markup" are intimately tied, and are both part of the "presentation" layer, so we're not breaking separation of concerns.
25
-- Large projects usually don't use WYSIWYG editors for production code, so breaking apart markup from the code that creates it usually only introduces friction.
26
-- We've built a safe, convenient and fast way to compose markup and components using pure JavaScript. This means no manual string concatenation and limited surface area for XSS vulnerabilities.
22
+Traditionally, you'd create a set of templates with a template
23
+language or HTML directives to make a page dynamic. It's up to the
24
+designer of the template language or the author of the directives to
25
+provide the full set of abstractions you can use to build your
26
+front-end code.
27
+
28
+React's technique is to break your view down into small, composable
29
+and reusable **components**. These components provide a `render()`
30
+method which specifies how the component will generate its
31
+markup. `render()` can either return normal DOM elements (like
32
+`<div>`s) or can return other components.
33
+
34
+This means that yes, we have JavaScript generating markup. But we
35
+think that this is an advantage over using templates for a few
36
+reasons:
37
+
38
+- **JavaScript is a flexible, powerful programming language with the
39
+ ability to build abstractions.** This is incredibly important in
40
+ large applications.
41
+- "Logic" and "markup" are intimately tied, and are both part of the
42
+ "presentation" layer, so we're not breaking separation of concerns.
43
+- Large projects usually don't use WYSIWYG editors for production
44
+ code, so breaking apart markup from the code that creates it usually
45
+ only introduces friction.
46
+- We've built a safe, convenient and fast way to compose markup and
47
+ components using pure JavaScript. This means **no manual string
48
+ concatenation** and limited surface area for XSS vulnerabilities.
49
50
## Reacting to changes
51
52
React really shines when your data changes over time.
53
32
-In a traditional JavaScript application you need to look at what data changed and imperatively make changes to the DOM to make them consistent. Even AngularJS, which provides a declarative interface via directives and data binding, still requires a linking function to manually update DOM nodes (remember: React components are quite flexible and analogous to AngularJS directives, not templates. In big apps you'll almost certainly need this level of expressive power).
54
+In a traditional JavaScript application you need to look at what data
55
+changed and imperatively make changes to the DOM to make them
56
+consistent. Even AngularJS, which provides a declarative interface via
57
+directives and data binding, still requires a linking function to
58
+manually update DOM nodes (remember: React components are quite
59
+flexible and analogous to AngularJS directives, not templates. In big
60
+apps you'll almost certainly need this level of expressive power).
61
62
React takes a different approach.
63
36
-When your component is first initialized, the `render()` method is called and a string of static HTML is inserted into the DOM. When your data changes, the `render()` method is called again. We diff the old return value from `render()` with the new one and determine the fastest way to update the DOM. So if only a single attribute on a single node has changed, that's all that React will update.
37
-
38
-We call this process **reconciliation**. Check out [this jsFiddle](http://jsfiddle.net/fv6RD/3/) for an example of reconciliation in action.
39
-
40
-Usually reconciliation will be faster than handwritten code, as React knows about the entire state of the page and can do cool tricks like batching reads and writes and picking the fastest subset of DOM mutations to perform.
41
-
42
-The way we're able to pull this off is by constructing a very fast, lightweight representation of the DOM which knows which parts are dirtied and which parts are clean. That is, **the data returned from `render()` isn't a string and isn't a real DOM node, it's just a lightweight description of what the DOM should look like**.
43
-
44
-Because this re-render is so fast (on the order of 1ms for TodoMVC), we don't need the end user to explicitly specify data bindings. We've found that this is an easy way to build apps. It's a lot like the early days of the dynamic web. Back then you wrote simple presentational code and when your data changed you simply refreshed the page. Today, React makes that "refresh" very fast and lightweight, and only changes the parts of the markup that need to be changed.
45
-
46
-No other framework we've seen can support this easily, since it would have to be built from the ground up to have very little coupling with the DOM.
64
+When your component is first initialized, the `render()` method is
65
+called and a string of static HTML is inserted into the DOM. When your
66
+data changes, the `render()` method is called again. We diff the old
67
+return value from `render()` with the new one and determine the
68
+fastest way to update the DOM. So if only a single attribute on a
69
+single node has changed, that's all that React will update.
70
+
71
+We call this process **reconciliation**. Check out
72
+[this jsFiddle](http://jsfiddle.net/fv6RD/3/) for an example of
73
+reconciliation in action.
74
+
75
+Usually reconciliation will be faster than handwritten code, as React
76
+knows about the entire state of the page and can do cool tricks like
77
+batching reads and writes and picking the fastest subset of DOM
78
+mutations to perform.
79
+
80
+The way we're able to pull this off is by constructing a very fast,
81
+lightweight representation of the DOM which knows which parts are
82
+dirtied and which parts are clean. That is, **the data returned from
83
+`render()` isn't a string and isn't a real DOM node, it's just a
84
+lightweight description of what the DOM should look like**.
85
+
86
+Because this re-render is so fast (on the order of 1ms for TodoMVC),
87
+we don't need the end user to explicitly specify data bindings. We've
88
+found that this is an easy way to build apps. It's a lot like the
89
+early days of the dynamic web. Back then you wrote simple
90
+presentational code and when your data changed you simply refreshed
91
+the page. Today, React makes that "refresh" very fast and lightweight,
92
+and only changes the parts of the markup that need to be changed.
93
+
94
+No other framework we've seen can support this easily, since it would
95
+have to be built from the ground up to have very little coupling with
96
+the DOM.
97
98
## Not just for HTML components in the browser
99
50
-Since React makes so few assumptions about its environment, we can do some pretty cool things with it:
51
-
52
-- Facebook.com has dynamic charts that render to `<canvas>` instead of HTML
53
-- Instagram is a "single page" web app built entirely with React and `Backbone.Router`. Designers regularly contribute React code with JSX.
54
-- We've built an internal prototype that runs React apps in a web worker
55
-- You can run React [on the server](http://github.com/petehunt/react-server-rendering) for SEO, performance, code sharing and overall flexibility.
56
-- Events behave in a consistent, standards-compliant way in all browsers (including IE8) and automatically use [event delegation](http://davidwalsh.name/event-delegate)
57
-
58
-Head on over to [facebook.github.io/react](http://facebook.github.io/react) to check out what we've built. Our documentation is geared towards building apps with the framework, but if you're interested in the nuts-and-bolts [get in touch](http://facebook.github.io/react/support.html) with us!
100
+Since React makes so few assumptions about its environment, we can do
101
+some pretty cool things with it:
102
+
103
+- Facebook.com has dynamic charts that render to `<canvas>` instead of
104
+ HTML
105
+- Instagram is a "single page" web app built entirely with React and
106
+ `Backbone.Router`. Designers regularly contribute React code with
107
+ JSX.
108
+- We've built an internal prototype that runs React apps in a web
109
+ worker
110
+- You can run React
111
+ [on the server](http://github.com/petehunt/react-server-rendering)
112
+ for SEO, performance, code sharing and overall flexibility.
113
+- Events behave in a consistent, standards-compliant way in all
114
+ browsers (including IE8) and automatically use
115
+ [event delegation](http://davidwalsh.name/event-delegate)
116
+
117
+Head on over to
118
+[facebook.github.io/react](http://facebook.github.io/react) to check
119
+out what we've built. Our documentation is geared towards building
120
+apps with the framework, but if you're interested in the
121
+nuts-and-bolts
122
+[get in touch](http://facebook.github.io/react/support.html) with us!
123
124
Thanks for reading!