Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/captivationsoftware/react-global
<Global /> component for isomorphic React apps
https://github.com/captivationsoftware/react-global
Last synced: 3 months ago
JSON representation
<Global /> component for isomorphic React apps
- Host: GitHub
- URL: https://github.com/captivationsoftware/react-global
- Owner: captivationsoftware
- License: mit
- Created: 2015-02-26T03:09:47.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-08-07T00:07:56.000Z (over 7 years ago)
- Last Synced: 2024-09-30T23:18:28.608Z (4 months ago)
- Language: JavaScript
- Size: 129 KB
- Stars: 22
- Watchers: 2
- Forks: 8
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
react-global
============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).
## Installation
```sh
npm install react-global
```
## Code Examples### Setting Global Values
```js
var Global = require('react-global');```
### Getting Global Values
```js
Global.get('FOO')
```Note: every Global variable that you declare will actually be available on the window object as a true browser global.
```js
Global.get('FOO') === window.FOO === window['FOO'] === FOO
```## Use Case
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.server.js
```js
var React = require('React'),
App = require('./views/App.jsx');app.get('/', function(req, res) {
var markup = React.renderToString();
res.send('' + markup);
});```
App.jsx
```js
var React = require('react'),
Global = require('react-global');var App = React.createClass({
componentDidMount: function() {
ClientLib.init(this.props.clientApiKey);
},render: function() {
...
...
}
});// Mount the app if in the browser
if (window && document) {
React.render(
}```
A simple but useful pattern!
## Contributors
Implementation by Captivation Software (@teamcaptivation), overall design by Adam Nalisnick (@theadam4257)
By all means, if you see room for improvement, let us know!
## License
MIT License