Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit a9a9154

Browse files
author
Ambroos Vaes
committed
Version 0.1 - fork from gaearon/react-document-title
1 parent 84be85f commit a9a9154

File tree

5 files changed

+64
-135
lines changed

5 files changed

+64
-135
lines changed

README.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
React Document Title
2-
====================
1+
React Nested Status
2+
===================
33

4-
Provides a declarative way to specify `document.title` in a single-page app.
5-
This component can be used on server side as well.
4+
A server-side utility component that passes status codes. Useful to set HTTP status codes based on which components are being rendered.
65

76
Built with [React Side Effect](https://github.com/gaearon/react-side-effect).
87

8+
Based (heavily) on [React Document Title](https://github.com/gaearon/react-document-title).
9+
910
====================
1011

1112
## Installation
1213

1314
```
14-
npm install --save react-document-title
15+
npm install --save react-nested-status
1516
```
1617

1718
Dependencies: React >= 0.11.0
1819

1920
## Features
2021

22+
* Made for isomorphic apps. Really doesn't have a use without server-side React.
2123
* Does not emit DOM, not even a `<noscript>`;
2224
* Like a normal React compoment, can use its parent's `props` and `state`;
2325
* Can be defined in many places throughout the application;
2426
* Supports arbitrary levels of nesting, so you can define app-wide and page-specific titles;
25-
* Works just as well with isomorphic apps.
2627

2728
## Example
2829

@@ -33,9 +34,9 @@ var App = React.createClass({
3334
render: function () {
3435
// Use "My Web App" if no child overrides this
3536
return (
36-
<DocumentTitle title='My Web App'>
37+
<NestedStatus code=200>
3738
<this.props.activeRouteHandler />
38-
</DocumentTitle>
39+
</NestedStatus>
3940
);
4041
}
4142
});
@@ -44,36 +45,36 @@ var HomePage = React.createClass({
4445
render: function () {
4546
// Use "Home" while this component is mounted
4647
return (
47-
<DocumentTitle title='Home'>
48+
<NestedStatus code=200>
4849
<h1>Home, sweet home.</h1>
49-
</DocumentTitle>
50+
</NestedStatus>
5051
);
5152
}
5253
});
5354

54-
var NewArticlePage = React.createClass({
55+
var ErrorPage = React.createClass({
5556
mixins: [LinkStateMixin],
5657

5758
render: function () {
5859
// Update using value from state while this component is mounted
5960
return (
60-
<DocumentTitle title={this.state.title || 'Untitled'}>
61+
<NestedStatus code=404>
6162
<div>
62-
<h1>New Article</h1>
63-
<input valueLink={this.linkState('title')} />
63+
<h1>Four-oh-four</h1>
64+
<p>Page not found.</p>
6465
</div>
65-
</DocumentTitle>
66+
</NestedStatus>
6667
);
6768
}
6869
});
6970
```
7071

7172
## Server Usage
7273

73-
If you use it on server, call `DocumentTitle.rewind()` after rendering components to string to retrieve the title given to the innermost `DocumentTitle`. You can then embed this title into HTML page template.
74+
Call `NestedStatus.rewind()` after rendering components to string to retrieve the status code given to the innermost `NestedStatus`. You can then use this to set your Express (or other web server) status code.
7475

7576
Because this component keeps track of mounted instances, **you have to make sure to call `rewind` on server**, or you'll get a memory leak.
7677

77-
## But What About Meta Tags?
78+
## Thank you
7879

79-
I want to keep this project simple and [after a discussion](https://github.com/gaearon/react-document-title/issues/5) decided it to be out of scope. The good news is you can implement this yourself using the same code that powers React Document Title: [React Side Effect](https://github.com/gaearon/react-side-effect). If figure out a good API for setting `<meta>`, `<link rel='canonical'>` and similar tags in a nested fashion and use React Side Effect for that, please let me know, so I can link to your project!
80+
A huge thanks to [gaearon](https://github.com/gaearon) for his open-source [React Document Title](https://github.com/gaearon/react-document-title) that was easy to understand and modify.

index.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,36 @@
33
var React = require('react'),
44
createSideEffect = require('react-side-effect');
55

6-
var _serverTitle = null;
6+
var _serverStatus = 200;
77

8-
function getTitleFromPropsList(propsList) {
8+
function getStatusFromPropsList(propsList) {
99
var innermostProps = propsList[propsList.length - 1];
1010
if (innermostProps) {
11-
return innermostProps.title;
11+
return innermostProps.code;
1212
}
1313
}
1414

15-
var DocumentTitle = createSideEffect(function handleChange(propsList) {
16-
var title = getTitleFromPropsList(propsList);
17-
18-
if (typeof document !== 'undefined') {
19-
document.title = title || '';
20-
} else {
21-
_serverTitle = title || null;
22-
}
15+
var NestedStatus = createSideEffect(function handleChange(propsList) {
16+
var status = getStatusFromPropsList(propsList);
17+
_serverStatus = status || 200;
2318
}, {
24-
displayName: 'DocumentTitle',
19+
displayName: 'NestedStatus',
2520

2621
propTypes: {
27-
title: React.PropTypes.string.isRequired
22+
code: React.PropTypes.number.isRequired
2823
},
2924

3025
statics: {
3126
peek: function () {
32-
return _serverTitle;
27+
return _serverStatus;
3328
},
3429

3530
rewind: function () {
36-
var title = _serverTitle;
31+
var status = _serverStatus;
3732
this.dispose();
38-
return title;
33+
return status;
3934
}
4035
}
4136
});
4237

43-
module.exports = DocumentTitle;
38+
module.exports = NestedStatus;

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
{
2-
"name": "react-document-title",
3-
"version": "1.0.2",
4-
"description": "A declarative, nested, stateful document.title for React",
2+
"name": "react-nested-status",
3+
"version": "0.1.0",
4+
"description": "A nested, stateful status code thing for React. Based on react-document-title.",
55
"main": "index.js",
66
"peerDependencies": {
7-
"react": ">=0.11.0 || >=0.13.0-beta.1"
7+
"react": ">=0.11.0 || >=0.13.0"
88
},
99
"scripts": {
1010
"test": "mocha",
1111
"lint": "jshint index.js test"
1212
},
1313
"repository": {
1414
"type": "git",
15-
"url": "https://github.com/gaearon/react-document-title.git"
15+
"url": "https://github.com/Ambroos/react-nested-status.git"
1616
},
1717
"keywords": [
1818
"react",
1919
"component",
2020
"react-component",
21-
"document.title",
22-
"title",
21+
"status",
22+
"express",
2323
"jsx"
2424
],
25-
"author": "Dan Abramov <dan.abramov@me.com> (http://github.com/gaearon)",
25+
"author": "Ambroos Vaes <ambroosv@gmail.com> (http://github.com/Ambroos)",
2626
"license": "MIT",
2727
"bugs": {
28-
"url": "https://github.com/gaearon/react-document-title/issues"
28+
"url": "https://github.com/Ambroos/react-nested-status/issues"
2929
},
30-
"homepage": "https://github.com/gaearon/react-document-title",
30+
"homepage": "https://github.com/Ambroos/react-nested-status",
3131
"devDependencies": {
3232
"expect.js": "^0.3.1",
3333
"global": "^4.3.0",
3434
"jshint": "^2.5.6",
3535
"mocha": "^2.0.1",
36-
"react": "^0.13.0-beta.1"
36+
"react": "^0.13.0"
3737
},
3838
"dependencies": {
3939
"react-side-effect": "~0.3.0"

test/browser.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

test/common.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
'use strict';
44
var expect = require('expect.js'),
55
React = require('react'),
6-
DocumentTitle = require('../');
6+
NestedStatus = require('../');
77

8-
describe('DocumentTitle', function () {
8+
describe('NestedStatus', function () {
99
it('has a displayName', function () {
10-
var el = React.createElement(DocumentTitle);
10+
var el = React.createElement(NestedStatus);
1111
expect(el.type.displayName).to.be.a('string');
1212
expect(el.type.displayName).not.to.be.empty();
1313
});
1414
it('hides itself from the DOM', function () {
1515
var Component = React.createClass({
1616
render: function () {
17-
return React.createElement(DocumentTitle, {title: 'irrelevant'},
17+
return React.createElement(NestedStatus, {code: 2727},
1818
React.createElement('div', null, 'hello')
1919
);
2020
}
@@ -25,7 +25,7 @@ describe('DocumentTitle', function () {
2525
it('throws an error if it has multiple children', function (done) {
2626
var Component = React.createClass({
2727
render: function () {
28-
return React.createElement(DocumentTitle, {title: 'irrelevant'},
28+
return React.createElement(NestedStatus, {code: 2727},
2929
React.createElement('div', null, 'hello'),
3030
React.createElement('div', null, 'world')
3131
);
@@ -49,7 +49,7 @@ describe('DocumentTitle', function () {
4949
});
5050
var Component2 = React.createClass({
5151
render: function () {
52-
return React.createElement(DocumentTitle, {title: 'irrelevant'},
52+
return React.createElement(NestedStatus, {code: 2727},
5353
React.createElement('div', null,
5454
React.createElement('div', null, 'a'),
5555
React.createElement('div', null, 'b'),
@@ -74,33 +74,33 @@ describe('DocumentTitle', function () {
7474
});
7575
});
7676

77-
describe('DocumentTitle.rewind', function () {
77+
describe('NestedStatus.rewind', function () {
7878
it('clears the mounted instances', function () {
7979
React.renderToStaticMarkup(
80-
React.createElement(DocumentTitle, {title: 'a'},
81-
React.createElement(DocumentTitle, {title: 'b'}, React.createElement(DocumentTitle, {title: 'c'}))
80+
React.createElement(NestedStatus, {code: 201},
81+
React.createElement(NestedStatus, {code: 202}, React.createElement(NestedStatus, {code: 203}))
8282
)
8383
);
84-
expect(DocumentTitle.peek()).to.equal('c');
85-
DocumentTitle.rewind();
86-
expect(DocumentTitle.peek()).to.equal(null);
84+
expect(NestedStatus.peek()).to.equal(203);
85+
NestedStatus.rewind();
86+
expect(NestedStatus.peek()).to.equal(200);
8787
});
88-
it('returns the latest document title', function () {
89-
var title = 'cheese';
88+
it('returns the latest status code', function () {
89+
var code = 200;
9090
React.renderToStaticMarkup(
91-
React.createElement(DocumentTitle, {title: 'a'},
92-
React.createElement(DocumentTitle, {title: 'b'}, React.createElement(DocumentTitle, {title: title}))
91+
React.createElement(NestedStatus, {code: 404},
92+
React.createElement(NestedStatus, {code: 500}, React.createElement(NestedStatus, {code: code}))
9393
)
9494
);
95-
expect(DocumentTitle.rewind()).to.equal(title);
95+
expect(NestedStatus.rewind()).to.equal(code);
9696
});
97-
it('returns nothing if no mounted instances exist', function () {
97+
it('returns 200 if no mounted instances exist', function () {
9898
React.renderToStaticMarkup(
99-
React.createElement(DocumentTitle, {title: 'a'},
100-
React.createElement(DocumentTitle, {title: 'b'}, React.createElement(DocumentTitle, {title: 'c'}))
99+
React.createElement(NestedStatus, {code: 500},
100+
React.createElement(NestedStatus, {code: 404}, React.createElement(NestedStatus, {code: 301}))
101101
)
102102
);
103-
DocumentTitle.rewind();
104-
expect(DocumentTitle.peek()).to.equal(null);
103+
NestedStatus.rewind();
104+
expect(NestedStatus.peek()).to.equal(200);
105105
});
106106
});

0 commit comments

Comments
 (0)