├── LICENSE
├── README.md
├── index.js
├── lib
└── global.js
└── package.json
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 captivationsoftware
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | react-global
2 | ============
3 |
4 | A simple React component for exposing global properties on your page. This library is particularly useful for isomorphic apps, where a set of values must be shared between the server and client (e.g. passing initialization variables to third-party libraries).
5 |
6 | ## Installation
7 | ```sh
8 | npm install react-global
9 | ```
10 | ## Code Examples
11 |
12 | ### Setting Global Values
13 |
14 | ```js
15 | var Global = require('react-global');
16 |
17 |
21 |
22 | ```
23 |
24 | ### Getting Global Values
25 | ```js
26 | Global.get('FOO')
27 | ```
28 |
29 | Note: every Global variable that you declare will actually be available on the window object as a true browser global.
30 | ```js
31 | Global.get('FOO') === window.FOO === window['FOO'] === FOO
32 |
33 | ```
34 |
35 | ## Use Case
36 | This component is meant to ease the case where your isomorphic app's server has some configuration variable's that need to be shared with the browser that will never change over the course of the user's interaction with your app. Below you will find a reusable pattern for accomplishing this task cleanly and concisely using react-global.
37 |
38 | server.js
39 | ```js
40 | var React = require('React'),
41 | App = require('./views/App.jsx');
42 |
43 | app.get('/', function(req, res) {
44 | var markup = React.renderToString();
45 | res.send('' + markup);
46 | });
47 |
48 | ```
49 |
50 | App.jsx
51 | ```js
52 | var React = require('react'),
53 | Global = require('react-global');
54 |
55 | var App = React.createClass({
56 |
57 | componentDidMount: function() {
58 | ClientLib.init(this.props.clientApiKey);
59 | },
60 |
61 | render: function() {
62 | ...
63 |
66 | ...
67 | }
68 | });
69 |
70 | // Mount the app if in the browser
71 | if (window && document) {
72 | React.render(
73 | }
74 |
75 | ```
76 |
77 | A simple but useful pattern!
78 |
79 | ## Contributors
80 |
81 | Implementation by Captivation Software (@teamcaptivation), overall design by Adam Nalisnick (@theadam4257)
82 |
83 | By all means, if you see room for improvement, let us know!
84 |
85 | ## License
86 |
87 | MIT License
88 |
89 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | module.exports = require('./lib/global.js');
2 |
--------------------------------------------------------------------------------
/lib/global.js:
--------------------------------------------------------------------------------
1 | var React = require('react');
2 |
3 | var Global = React.createClass({
4 | statics: {
5 | get: function(name) {
6 | return window[name];
7 | }
8 | },
9 |
10 | shouldComponentUpdate: function() {
11 | return false;
12 | },
13 |
14 | getScript: function() {
15 | var script = '';
16 | for (var key in this.props.values || {}) {
17 | script += 'var ' + key + '=' + JSON.stringify(this.props.values[key]) + ';';
18 | }
19 | return script;
20 | },
21 |
22 | render: function() {
23 | return React.createElement("script", {
24 | type: "text/javascript",
25 | dangerouslySetInnerHTML: {
26 | __html: this.getScript()
27 | }
28 | });
29 | }
30 | });
31 |
32 | module.exports = Global;
33 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-global",
3 | "version": "0.1.8",
4 | "description": "Global variables for isomorphic React applications",
5 | "main": "./index",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "https://github.com/captivationsoftware/react-global"
12 | },
13 | "keywords": [
14 | "React",
15 | "Constant",
16 | "Global",
17 | "Isomorphic"
18 | ],
19 | "author": "Captivation Software, c/o @theadam",
20 | "license": "MIT",
21 | "bugs": {
22 | "url": "https://github.com/captivationsoftware/react-global/issues"
23 | },
24 | "homepage": "https://github.com/captivationsoftware/react-global",
25 | "peerDependencies": {
26 | "react": "0.x"
27 | }
28 | }
29 |
--------------------------------------------------------------------------------