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

federal

v0.0.2

Published

Minimalistic centralized React store

Downloads

5

Readme

Federal

Minimalistic centralized React store

Why?

Federal wraps your react components, creating a centralized data store. It's similar to Redux, in a lot of ways.

Redux is great, and is often the right choice. But, sometimes it adds too much complexity to set up a simple page; As in their todo list example, there's a main index.js, actions/index.js, reducers/todo.js, reducers/visibilityFilter.js, reducers/index.js, and finally the presentation components. All for a simple todo.

Federal takes the 'less is more' approach. In any file that uses Federal, you simply need to provide an intial store object, and an object of actions (e.g. actions/index.js). Then, you can connect() any component to the store, which will result in it receiving store .props, including .props.dispatch for each action method (e.g. this.props.dispatch.setStatus('status')).

Can I use this in Production?

Of course! It's currently being used, in production, on Conjure.

Setup

npm install --save federal

Use

Simple Example

This example uses Federal to wrap a page content with a centralized store, and then has a child component (<Header />) connect to the store, which allows it to render the user's name.

pages/dashboard/index.js

import Federal from 'federal';
import Header from '../../components/Header';

// assume `account` is passed in as a prop
export default ({ account }) => {
  const initialStore = {
    account
  };

  return (
    <Federal store={initialStore}>
      <Header />
    </Federal>
  );
};

components/Header/index.js

import { connect } from 'federal';

const Header = ({ account }) => (
  <header>
    <dl>
      <dt>User</dt>
      <dd>{account.name}</dd>
    </dl>
  </header>
);

// using `connect()` will bind `<Header />` to the full store object
export default connect()(Header);

Selectors

Let's say the above example's initialStore changes to something like this:

const initialStore = {
  account,
  products,
  notices
};

In this case, you don't want or need products or notices in order to render <Header />. You can use a selector to minimize the scope of the store changes passed to a component. Selectors are passed to connect().

const selector = store => ({
  account: store.account
});

export default connect(selector)(Header);

Actions

Adding actions allows you to dispatch a change to the central store. A dispatch will then ripple and update to any subscribed components.

Actions must return a new object, or Federal will consider nothing to have changed.

pages/dashboard/index.js

import Federal from 'federal';
import CountSummary from '../../components/CountSummary';
import CountInteractions from '../../components/CountInteractions'
import actions from './actions';

export default () => {
  const initialStore = {
    count: 0
  };

  return (
    <Federal store={initialStore} actions={actions}>
      <CountSummary />
      <CountInteractions />
    </Federal>
  );
};

pages/dashboard/actions/index.js

const resetCount = store => {
  return Object.assign({}, store, {
    count: 0
  });
};

// second arg to each action is an object, that can be passed when dispatching
const addToCount = (store, { addition }) => {
  return Object.assign({}, store, {
    count: store.count + addition
  });
};

export default {
  resetCount,
  addToCount
};

components/CountSummary/index.js

import { connect } from 'federal';

const CountSummary = ({ count }) => (
  <div>Current count is {count}</div>
);

export default connect()(CountSummary);

components/CountInteractions/index.js

import { connect } from 'federal';

// `connect()` passed a `dispatch` prop that exposes all actions to the component
const CountInteractions = ({ dispatch }) => (
  <div>
    <div>
      <button
        type='button'
        onClick={() => {
          dispatch.addToCount({
            addition: 1 // can be modified to increment faster
          });
        }}
      >
        increment count
      </button>
    </div>
    <div>
      <button
        type='button'
        onClick={() => {
          dispatch.resetCount();
        }}
      >
        reset count
      </button>
    </div>
  </div>
);

export default connect()(CountInteractions);

Action Callbacks

Actions have an optional callback.

dispatch.addToCount({
  addition: 1
}, () => {
  // ...
});

Actions via connect()

A component may have local actions, while <Federal> is rendered at a different level in the layout. You can append action handlers to the connected components.

const removeFromCount = (store, { deduction }) => {
  return Object.assign({}, store, {
    count: store.count - deduction
  });
};

// action .removeFromCount() added to CountSummary.props.dispatch,
// while still including all root-level actions
export default connect(state => state, { removeFromCount })(CountSummary);