Giter VIP home page Giter VIP logo

react-global's Introduction

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

npm install react-global

Code Examples

Setting Global Values

var Global = require('react-global');

<Global values={{
  FOO: this.props.foo,
  BAR: this.props.bar
}} />

Getting Global Values

Global.get('FOO')

Note: every Global variable that you declare will actually be available on the window object as a true browser global.

  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

var React = require('React'),
  App = require('./views/App.jsx');

app.get('/', function(req, res) {
  var markup = React.renderToString(<App clientApiKey={config.client.api.key} />);
  res.send('<!DOCTYPE html>' + markup);
});

App.jsx

var React = require('react'),
  Global = require('react-global');

var App = React.createClass({

  componentDidMount: function() {
    ClientLib.init(this.props.clientApiKey);    
  },

  render: function() {
    ...
      <Global values={{
        CLIENT_API_KEY: this.props.clientApiKey
      }} />
    ...
  }
});

// Mount the app if in the browser
if (window && document) {
  React.render(<App clientApiKey={Global.get('CLIENT_API_KEY')} />
}

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

react-global's People

Contributors

dbarbalato avatar devniel avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.