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

react-relax

v1.1.2

Published

A minimal way of providing redux state to react components that doesn't use context

Downloads

14

Readme

react-relax

A minimal way of providing redux state to react components that doesn't use context

Installation: npm install react-relax

Meta

  • license: MIT

globalStateProvider

An instance of StateProvider for convenience

Examples

import { globalStateProvider } from 'react-relax';

setStore

Set the store and mount point for the default global StateProvider

Parameters

  • store Redux.Store
  • mountPoint [string]

Examples

import { setStore } from 'react-relax';
setStore(reduxStore, 'myFancyMountPoint');

selectFromStore

The globalStateProvider selectFromStore method

Examples

import { selectFromStore } from 'react-relax';
selectFromStore({ language: 'viewer.language', name: 'user.name' });
// > { language: 'en', name: 'Apathy Hives' };

observe

The globalStateProvider observe method

Examples

import { observe } from 'react-relax';
const unsubscribe = observe(() => ({ stock: ['product', id, 'stockLevel'] }), ({ stock }) => {
  alert(`Stock level changed: ${stock}`);
});

// Later:
unsubscribe();

provideState

The globalStateProvider provideState higher order component

Examples

import provideState from 'react-relax';
function MyComponent({ userName, onChange }) {
  return (
    <label>
      Update username for {userId}: <input type="text" value={userName} onChange={onChange} />
    </label>
  );
}

const WrappedComponent = provideState(MyComponent, {
  within: (props) => ['users', props.userId],
  bindings: {
    userName: ['info.name'],
  },
  actions: {
    onChange: (event, props) => ({
      type: 'UPDATE_USER_NAME',
      userId: props.userId,
      name: event.target.value,
    }),
  },
});

React.render(<WrappedComponent userId="123" />);

StateProvider

The StateProvider class includes everything that the provideState function uses, scoped to a single redux store + mount point.

With no mountPoint specified, your entire store state should be an Immutable.JS collection. When mountPoint is specified, we assume that your store state is a plain JS object, but that that key refers to an Immutable.JS collection.

Parameters

  • store Redux.Store the redux store you want to use the state of
  • mountPoint [string] the key under the redux store you wish to use.

Examples

import { StateProvider } from 'react-relax';
const stateProvider = new StateProvider(store, 'immutableMountPoint');

function MyComponent({ name }) {
  return <span>User: {name}</span>;
}

export default stateProvider.provideState(MyComponent, {
  within: ({ userId }) => ['users', userId],
  bindings: {
    name: 'name',
  },
});

StateProvider.getState

Get the immutable collection from the store backing this StateProvider, using the mountPoint if specified

Returns Immutable.Collection

StateProvider.selectFromStore

Select props from store using a selectors object

Parameters

  • selectors Object an object of prop names to selector paths

Examples

stateProvider.selectFromStore({
  name: 'user.name',
  gender: ['user', 'gender'],
});
// > { name: 'Salsa', gender: 'Spiders' }

Returns {$name: any} an object mapping the keys from selectors to values from the store

StateProvider.observe

Observe a set of selectors as the store changes

Parameters

  • resolveSubscriptions {Function} - a function returning a set of selectors that can be passed to StateProvider.selectFromStore
  • onChange {Function} - called with the new values whenever they change. Although this uses Redux.Store.subscribe to handleChanges under the hood, onChange will only be called if the resolved values are different from their state in the previous store.

Returns Function The unsubscribe function

StateProvider.provideState

A higher-order react component that provides state props into the wrapped component

Parameters

  • Component React.Component the Component to wrap
  • options [Object] options detailing which props to provide and how to resolve them (optional, default {})
    • options.bindings [Object] an object mapping prop names to selectors for the store. When a function is provided, it will be called with props and should return a selector. (optional, default {})
    • options.within [(string | Array | Immutable.Iterable | Function)] Optionally scope all bindings to be beneath this selector (optional, default [])
    • options.actions [Object] An object mapping prop names to functions that return redux actions to be dispatched when they are called (optional, default {})

Returns React.Component The wrapped component

formatSelector

Normalize a selector into an array or immutable iterable

Parameters

  • selector (string | Array | Immutable.Iterable) The selector to normalise. If a string, sections should be seperated by dots.

Examples

import { formatSelector } from 'react-relax';
formatSelector('todos.0.name');
// > List('todos', '0', 'name');
formatSelector(['icecreams', 'chocolate'])
// > List('icecreams', 'chocolate')

Returns Immutable.Iterable The exploded path for this selector

joinSelectors

Join several selectors into one selector

Parameters

  • selectors ...(string | Array | Immutable.Iterable) the selectors to join

Examples

import { joinSelectors } from 'react-relax';
joinSelectors('a.b', ['c', 'd'], List(['e', 'f']));
// > List('a', 'b', 'c', 'd', 'e', 'f')

Returns Any Immutable.List