GithubHelp home page GithubHelp logo

capsulehealth / redux-namespace Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evanrs/redux-namespace

1.0 4.0 0.0 51 KB

Namespace component state into a Redux store

License: MIT License

JavaScript 100.00%

redux-namespace's Introduction

Redux Namespace

Dead simple tool moving component local state to a Redux store namespace.

npm install --save redux-namespace

Motivation

Transient state like toggles and form input require too much boiler plate.

redux-namespace helps you connect your component with a trivial key value store. This solves the far–too–painful/should–be–easier problems with managing view state after routing.

Why store component state outside the component?

To get the incredible time traveling super powers and retained transients provided from having complete hydration of your app state. Time travel is fun, play with this.

Redux Namespace builds on React Redux, but provides new methods: assign, cursor and select to go with dispatch.

  • Calling assign(key, value) puts a value in your namespace and select(key) gets it out.
  • Calling select() with no parameters returns the entire namespace.
  • Calling cursor(key) returns a nested namespace.

Connecting your components with their own namespace is trivial. Use it like React Redux, but forget about writing the selectors.

namespace.connect('recipe/editor')(Component)

Installation

npm install --save redux-namespace

Attach the Reducer

import { createStore, combineReducers } from 'redux';
import namespace from 'redux-namespace';

const store = createStore(combineReducers({namespace}));

Usage

Native

import React, {View, Text, TextInput, TouchableHighlight, } from 'react-native'
import namespace from 'redux-namespace/native';

// If decorators are your thing
@namespace.connect('component/namespace')
class Form extends React.Component {
  static propTypes = {...namespace.shape}

  render () {
    let {select, assign, dispatch} = this.props;
    return (
      <View>
        <TextInput
          value={select('email')} onChange={assign('email')}/>

        <TextInput
          value={select('password')} onChange={assign('password')}/>

        <TouchableHighlight onPress={e => dispatch(someAction(select()))}>
          <Text>Submit</Text>
        </TouchableHighlight>
      </View>
    )
  }
}

Web

import React from 'react'
import namespace from 'redux-namespace';

class Form extends React.Component {
  static propTypes = {...namespace.shape}

  render () {
    let {select, assign, dispatch} = this.props;
    return (
      <form onSumbit={() => dispatch(someAction(select()))}/>
        <input
          value={select('email')}
          onChange={e => assign('email', e.target.value)}/>
        <input
          value={select('password')}
          onChange={e => assign('password', e.target.value)}/>
      </form>
    )
  }
}

// Or, if decorators aren't your thing
export namespace.connect('component/namespace')(Form)

Routing

<Route path='free-pizza'
  component={ namespace.connect('pizza-surplus')(Component) }/>
You get a namespace, and you get a namespace … !

Automatically wrap your routes by assigning them after they're made.

let routes = [
  <Route/>
  <Route/>
  <Route/>
]

routes = routes.map(route => namespace.connect(route.path)(route))

Lazy binding… sort of

@namespace.connect('recipes')
class RecipeEditor extends React.Component {
  static propTypes = {...namespace.shape}

  render () {
    let document = namespace.connect(`recipes/${select('currentId')}`);
    let autosave = namespace.connect(`recipes/${select('currentId')}/autosave`);
    let groceries = namespace.connect('groceries');

    let Loader = document(Await)
    let Autosave = document(autosave(DebouncedSave))

    return (
      <Loader>
        <Autosave/>
        { map(React.createElement,
            map(document, [
              Menu,
              WYSIWYG,
              Editor,
              WordCount,
              groceries(VegetableCount),
              groceries(CalorieCount),
              groceries(CountChoculaCount)
            ]))
          }
      </Loader>
    )
  }
}

Skip writing actions

Using the namespace.BIND constant you can write new reducers to work with your events without adding more plumbing.

function (state, action) {
  if (action.type === namespace.BIND) {
    let {namespace, key, value} = action.payload;

    switch (namespace) {
      case 'app/signup':
        state[namespace][key].error = validate(key, value.field);
        break;

      case 'my/easter/egg':
        if (key === 'menuToggle')
          if (++state.finalCountdown > 5)
            state.surpriseCatGif = true
        break;
    }

    // I'm not sure which is more esoteric, the previous example or this one
    if (state.namespace[namespace][ttl] > new Date() - state[ttl][action.namespace][key])
      delete state.namespace[namespace][key];
  }

  return state;
}

Although you probably shouldn't do this often, it's faster than scaffolding the additional constants and action creators. Beware, with great power comes greater side effects… that we probably should avoid unless we liked Angular 1—better known as Whoopsie Daisy.

License

MIT

redux-namespace's People

Contributors

evanrs avatar morenoh149 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  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.