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

@retailmenot/redux-mount-store

v1.1.3

Published

Store enhancer for redux adding mounting capabilities

Downloads

3

Readme

redux-mount-store

Store enhancer for redux adding mounting capabilities

Gist

Mount to a slice of the state and define data to view from that state. Receive a store creator. That store, when created, will receive the viewed data in its reducer and via getState().

const withMounts = require('@rmn/redux-mount-store');
const redux = require('redux');

const mountableStore = redux.createStore(
  (state, action) => state,
	{
		todos: [
			{
				description: 'Pet a dog',
				done: true
			},
			{
				description: 'Pet a frog',
				done: false
			}
		]
	},
	withMounts
);

const createMountedStore = mountableStore.mount(
  // the key in the mountable store where any non-virtual data will be stored
	'someComponent',
  // define a mapping from the mountable store's state to the mounted store's
  // virtual (or "viewed") state
	{
		completedTodos: state => state.todos.filter(todo => todo.done)
	}
);

// same signature as `createStore()`
const mountedStore = createMountedStore(
  // this reducer receives the virtual state and its own state merged together.
  // mutations to any virtual state will throw an error
	(state, action) => state,

  // the mounted store's own initial state. this will be persisted to the "real"
  // store.
	{
		isDropdownVisible: false
	}
);

Let's call getState() on each store. The state that we see is the same state provided to each store's reducer.

console.log(mountableStore.getState());

// note that a slice of the mountable store is created for the mounted store,
// containing its initial state. note that it does NOT contain the virtual
// state, which is instead provided to the mounted store's reducer and when its
// `getState()` method is called.
// { todos:
//    [ { description: 'Pet a dog', done: true },
//      { description: 'Pet a frog', done: false } ],
//   someComponent: { isDropdownVisible: false } }

console.log(mountedStore.getState());

// the mounted store sees its full state as well as its virtual/viewed state.
// in this case, it can update `isDropdownVisible` in its own reducer and that
// change will be visible in both stores. however, attempts to update
// completedTodos will raise an Error
// { isDropdownVisible: false,
//   completedTodos: [ { description: 'Pet a dog', done: true } ] }

Why?

In certain cases, this can be easier than simply mapping the store's state to the shape expected by consumers. Additionally, abstracting the layer at which the "real" store exists allows it to be plugged it any layer.

Consuming the redux-mount-store package

There are two ways of consuming @retailmenot/redux-mount-store within your application.

By default, when you require('@retailmenot/redux-mount-store'). You get the redux- mount-store source code, which is written using ES6 features such as fat arrow functions.

redux-mount-store also publishes an ES5 compatible version of itself to the dist/ directory, which can be consumed by applications that are using webpack without babel by aliasing @retailmenot/redux-mount-store to the transpiled code. In your webpack config:

resolve: {
    alias: {
        '@retailmenot/redux-mount-store': path.join(
            path.dirname(
                require.resolve('@retailmenot/redux-mount-store')
            ), 'dist/index.js'
        )
    }
}

The downside to this approach is that sourcemaps in your consuming application will point to the transpiled code instead of the redux-mount-store source.