npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-namespace

v1.0.3

Published

Namespace your component state in your Redux store

Downloads

154

Readme

Redux Namespace

Dead simple tool moving component local state into a Redux namespace.

npm install --save redux-namespace

Motivation

Got transient state without a home? Do your components lose it when they unmount? Are you swimming in a pool of reducers that do one thing? Then Redux Namespace is for you, because all those problems are tedious and boring, and you have better things to do!

redux-namespace it's a key value store, with depth.

Usage

yarn add redux-namespace

Attach the Reducer

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

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

Connect your components

This is probably too easy. Just name your namespace, then select and assign how you like.

connect('pizza')(({ pizza }) =>
  <input 
    value={pizza.select('delivery.time') 
    onChange={pizza.assign('delivery.time', 'target.value')) />)

Let's look at that again.

import * as namepsace from 'redux-namespace';

const Form = namespace.connect('form', 'signin')((props) => {
  let { form } = props;
  return (
    <View>
      <TextInput
        value={form.select('email')} onChange={form.assign('email')}/>

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

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

But you know what's up, assign is returning a function. A funtion that sets the path you give it to the value it gets.

But it's not always that easy, sometimes we have to be picky.

<input onChange={ns.assigns('email', 'target.value')}/>

And sometimes we have to be even pickier than that.

<CustomInput onChange={ns.assigns('email', (event, value) => value)}/>

How about lists? We can pick a value from props, or pass it a string. So connect('list', 'item') will become a prop called list, but its values will be assigned to list.item.

import { connect, shape } from 'redux-namespace'

@connect('list', (props) => props.id || 'new')
class ProductForm extends Component {
  static propTypes = {
    productsList: shape
  }

  render () {
    let { list: ns } = this.props;
    return (
      <form onSumbit={() => ns.dispatch(someAction(ns.select()))}/>
        <input
          value={ns.select('product.name')}
          onChange={ns.assigns('product.name', 'target.value')}/>
        <input
          value={ns.select('product.price')}
          onChange={ns.assigns('product.price', 'target.value')}/>
      </form>
    )
  }
}

But you don't have to manage it in one place. You can create a cursor—a pointer to one part of your namespace. It has all the same functions, but applied to its own descendant path.


const productList = [
  { id: 1, name: '🦄', price: '🌈' },
  { id: 2, name: '🐿', price: '🥜' },
  { id: 3, name: '🐮', price: '🌾' },
]

@connect('productsList')
const ProductManager = (props) => 
  <div>
    {productList.map((product) => {
        // This will alway return the same cursor
        const cursor = props.productList.cursor(product.id);
        
        // We don't need to set it everytime, but it's safe to
        cursor.defaults(product);
        
        return <ProductForm product={cursor}/>
      }
    )}
  </div>

You can also reset your namespace, see if it was touched and track its version. See the full API here.

It's not the Redux reducer we need, but it's the one I wrote, so have fun with it. ✌️

Psst.

Know how to make it the reducer we need?

Get in touch, let's make it work!