You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Well, before we start building anything meaningful, its important that we cover some base concepts first, so lets getting start.
11
11
12
-
### What is React?
13
12
14
-
React is a UI library developed at Facebook to facilitate the creation of interactive, stateful and reusable UI components. It is used at Facebook in production, and Instagram.com is written in React.
13
+
## What is React?
14
+
15
+
React is a UI library developed at Facebook to facilitate the creation of interactive, stateful and reusable UI components. It is used at Facebook in production, and Instagram is written in React.
15
16
16
17
One of it’s unique points is that not only does it perform on the client side, but it can also be rendered server side, and they can work together inter-operably.
17
18
18
-
###Concepts
19
+
## Concepts
19
20
20
21
React has quite a small API. This makes it fun to use, easy to learn, and simple to understand. However, being simple does not mean it’s familiar. There are a few concepts to cover before getting started. Let’s look at each in turn:
21
22
22
-
####React Elements
23
+
### React Elements
23
24
24
25
React elements are JavasScript objects which represent HTML elements. They don’t exist in the browser. They represent browser elements such as an h1, div or section.
25
26
26
-
####JSX
27
+
### JSX
27
28
28
29
JSX is a technique for creating React elements and components.
29
30
30
-
####Virtual DOM
31
+
### Virtual DOM
31
32
32
33
The Virtual DOM is a JavaScript tree of React elements and components. React render the Virtual DOM to the browser to make the user interface visible. React observes the Virtual DOM for changes and automatically mutates browser DOM to mach the Virtual DOM.
33
34
34
35
With a small understanding of these concepts we can move to using React. We will build a series of user interfaces, each adding a layer of functionality on the previous. We will build a photo stream similar to instagram.
35
36
36
-
####Rendering
37
+
### Rendering
37
38
38
39
The first order of business is rendering a virtual element (a React element or component). Remember, since a virtual element exists only in JavaScript memory, we must explicitly tell React to render it to the browser DOM.
The render function accepts two arguments, a virtual element and a real DOM node. React takes the virtual element and inserts it into the given DOM node.
45
46
46
-
####Components
47
+
### Components
47
48
48
49
Components are the heart and soul of React. They are custom React elements. They are usually extended with unique functionality and structure.
49
50
50
-
```
51
+
```javascript
51
52
var Photo =React.createClass({
52
53
render:function() {
53
54
return<img src='src/logo.png'/>;
@@ -63,11 +64,11 @@ The Photo component is constructed and rendered to the document body.
63
64
64
65
This component does nothing more than the previous React image element but it’s ready to be extended with custom functionality and structure.
65
66
66
-
####Props
67
+
### Props
67
68
68
69
Props can be thought of as a component’s options. They are given as arguments to a component and look exactly like HTML attributes.
69
70
70
-
```
71
+
```javascript
71
72
var Photo =React.createClass({
72
73
render:function() {
73
74
return (
@@ -82,11 +83,11 @@ var Photo = React.createClass({
The render method is the only required spec for creating a component, but there are serveral lifecycle methods & specs we can use that are mighty helpful when you actually want your component to do anything.
88
89
89
-
Lifecycle Methods:
90
+
#### Lifecycle Methods:
90
91
91
92
*`componentWillMount` - Invoked once, on both client & server before rendering occurs.
92
93
@@ -96,19 +97,19 @@ Lifecycle Methods:
96
97
97
98
*`componentWillUnmount` - invoked prior to unmounting component.
98
99
99
-
Specs:
100
+
#### Specs:
100
101
101
102
*`getInitialState` - Return value is the initial value for state.
102
103
103
104
*`getDefaultProps` - Sets fallback props values if props are not supplied.
104
105
105
106
*`mixins` - An array of objects, used to extend the current component’s functionality.
106
107
107
-
State
108
+
#### State
108
109
109
110
The state object is internal to a component. It holds data which can change over time.
110
111
111
-
```
112
+
```javascript
112
113
var Photo =React.createClass({
113
114
getInitialState:function() {
114
115
return {liked:false};
@@ -155,11 +156,11 @@ Here’s what happens when the component is rendered to the browser DOM:
155
156
156
157
* React isolates what has changed and updates the browser DOM.
157
158
158
-
####Composition
159
+
### Composition
159
160
160
161
Composition means combining smaller components to form a larger whole. For example the Photo component could be used inside a PhotoGallery component.
161
162
162
-
```
163
+
```javascript
163
164
var Photo =React.createClass({
164
165
toggleLiked:function() {
165
166
this.setState({liked:!this.state.liked});
@@ -218,11 +219,11 @@ There’s a new PhotoGallery component which generates Photo components. In this
218
219
219
220
The data is looped over and will generates 2 Photo components which are inserted into the return value of the component’s render function.
220
221
221
-
####Events
222
+
### Events
222
223
223
224
React also has a built in cross browser events system. The events are attached as properties of components and can trigger methods. Lets make our count increment below using events:
0 commit comments