Coder Social home page Coder Social logo

flux-constants's Introduction

React/Flux Constants

A bit 'o sugar for those looking to quickly create Flux style constants.

Installation:

Two options:

Option 1: npm

npm install --save flux-constants

Option 2: clone

git clone https://github.com/boichee/flux-constants

Having used React + Flux and typed this far too many times:

// ComponentConstants.js
var keyMirror = require('key-mirror');

module.exports = keyMirror({
  "SOME_CONSTANT": null,
  "ANOTHER_CONSTANT": null
});

I decided I hated having to type null over and over again for no reason.

So, I created this module as an alternative. Now you can do:

// ComponentConstants.js
module.exports = require('flux-constants')([
  "SOME_CONSTANT",
  "ANOTHER_CONSTANT"
]);

Ultimately, the result is the same: A JS object that looks like this: { SOME_CONSTANT: "SOME_CONSTANT", ANOTHER_CONSTANT: "ANOTHER_CONSTANT" }

Usage:

First, create your Flux Constants file:

SomeFluxConstants.js

module.exports = require('flux-constants')([
  'MY_FIRST_CONSTANT',
  'MY_SECOND_CONSTANT'
]);

Then use the constants like this:

SomeFluxActionCreator.js

var ComponentConstants = require('../constants/ComponentConstants')

var FluxActions = {
  doSomethingFluxLike: function(data) {
    var action = {
      type: ComponentConstants.MY_FIRST_CONSTANT,
      data: data
    };
    
    AtlasDispatcher.handleAction(action);
  }
};

Enjoy!

flux-constants's People

Contributors

boichee avatar

Stargazers

Andrey Stepanov avatar Miles avatar Zhang Xiaohui avatar Stephen Burgess avatar Florian Wendelborn avatar

Watchers

James Cloos avatar  avatar  avatar

flux-constants's Issues

Allow passing of arguments ES6 style

Currently, using react-constants means passing an array of strings. When you only have a few constants to add, this means extra typing and extra lines of code.

Add support for ES6 style Rest params so a user can pass individual params.

Type checking

Right now, getting this module to behave correctly is on the developer using it. I think this react helper should do a little work to let a developer know when the constants being created are not going to work as expected or as the Flux architecture has dictated.

Add input type checking to:

  • Make sure an array is being passed
  • Make sure the content of the array is comprised of strings (ignore all others and warn)

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.