Skip to content

Commit 2cde6ff

Browse files
committed
Merge pull request #29 from petehunt/mixin-docs
[docs] Return of mixin docs
2 parents a808d48 + f586c58 commit 2cde6ff

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-9
lines changed

docs/_config.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
---
2-
pygments: true
1+
---
2+
markdown: redcarpet
33
name: React
4+
redcarpet:
5+
extensions:
6+
- fenced_code_blocks
47
react_version: 0.3.0
5-
exclude:
8+
pygments: true
9+
exclude:
610
- Gemfile
711
- Gemfile.lock
812
- README.md
913
- Rakefile
10-
redcarpet:
11-
extensions:
12-
- fenced_code_blocks
13-
markdown: redcarpet
1414
baseurl: /react

docs/_includes/nav_docs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ <h3>Reference</h3>
1717
<li><a href="/react/docs/component-lifecycle.html"{% if page.id == 'docs-component-lifecycle' %} class="active"{% endif %}>Component Lifecycle</a></li>
1818
<li><a href="/react/docs/event-handling.html"{% if page.id == 'docs-event-handling' %} class="active"{% endif %}>Event Handling</a></li>
1919
<li><a href="/react/docs/advanced-components.html"{% if page.id == 'docs-advanced-components' %} class="active"{% endif %}>Advanced Components</a></li>
20+
<li><a href="/react/docs/mixins.html"{% if page.id == 'docs-mixins' %} class="active"{% endif %}>Mixins</a></li>
2021
<li><a href="/react/docs/api.html"{% if page.id == 'docs-api' %} class="active"{% endif %}>API</a></li>
2122
</ul>
2223
</div>

docs/docs/advanced-components.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Advanced Components
44
description: How to build advanced composite components.
55
layout: docs
66
prev: event-handling.html
7-
next: api.html
7+
next: mixins.html
88
---
99

1010
Composite components extend a `ReactCompositeComponent` base class that provides

docs/docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
id: docs-api
33
title: React API
44
layout: docs
5-
prev: advanced-components.html
5+
prev: mixins.html
66
---
77

88
## React

docs/docs/mixins.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
id: docs-mixins
3+
title: Mixins
4+
layout: docs
5+
prev: advanced-components.html
6+
next: api.html
7+
---
8+
9+
Mixins allow code to be shared between multiple React components. They are pretty similar to mixins
10+
in Python or traits in PHP. Let's look at a simple example:
11+
12+
```javascript
13+
var MyMixin = {
14+
getMessage: function() {
15+
return 'hello world';
16+
}
17+
};
18+
19+
var MyComponent = React.createClass({
20+
mixins: [MyMixin],
21+
render: function() {
22+
return <div>{this.getMessage()}</div>;
23+
}
24+
});
25+
```
26+
27+
A class can use multiple mixins, but no two mixins can define the same method. Two mixins can, however,
28+
implement the same [lifecycle method](component-lifecycle.html). In this case, each implementation will be invoked one after another.
29+
30+
The only exception is the `shouldComponentUpdate` lifecycle method. This method may only be implemented once
31+
(either by a mixin or by the component).
32+
33+
```javascript
34+
var Mixin1 = {
35+
componentDidMount: function() {
36+
console.log('Mixin1.componentDidMount()');
37+
}
38+
};
39+
40+
var Mixin2 = {
41+
componentDidMount: function() {
42+
console.log('Mixin2.componentDidMount()');
43+
}
44+
};
45+
46+
47+
var MyComponent = React.createClass({
48+
mixins: [Mixin1, Mixin2],
49+
render: function() {
50+
return <div>hello world</div>;
51+
}
52+
});
53+
```
54+
55+
When `MyComponent` is mounted into the page, the following text will print to the console:
56+
57+
```
58+
Mixin1.componentDidMount()
59+
Mixin2.componentDidMount()
60+
```
61+
62+
## When should you use mixins?
63+
64+
In general, add a mixin whenever you want a component to share some utility methods, public interface,
65+
or lifecycle behavior. Often it's appropriate to use them as you would use a superclass in another OOP language.

0 commit comments

Comments
 (0)