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

qaf

v0.0.11

Published

Components as stores. State management for React based on context.

Downloads

3

Readme

Qaf

npm version Build Status

Features

  • Based on React's new context API (16.3.0).
  • Every store is a React component.
  • Actions, lifecycle methods and more.
  • No dependencies, all just React goodness.
  • ~2 KB in size, with less than 100 lines of code.

Design

  • The app has at least one store, which is just a React component.
  • The app has at least one container (default), which is a collection of store instances.
  • Each container has a provider that exposes its store instances to its subscribers.
  • Any component can subscribe to any store instance provided.

Install

yarn add qaf

Usage

The store

import { createStore } from 'qaf';

// this creates a store with context hooks, we should do it uniquely for every store
const QafStore = createStore();

// every store is a typical React PureComponent
class Store extends QafStore {}

// or invoke directly
class Store extends createStore() {
  // components subscribing to the store will have access to everything in its state
  state = {
    // state values
    counter: 0,

    // actions are regular functions
    inc: () => this.setState(state => ({ counter: state.counter + 1 })),
    dec: () => this.setState(state => ({ counter: state.counter - 1 }))
  };

  // lifecycle methods
  componentDidMount() {
    // e.g. make an async call
  }

  // NOTE: don't declare `render`, Qaf will take care of that
}

The component

import { Subscriber, subscribe } from 'qaf';

const Counter = ({ store }) => (
  <div>
    {/* state values are available */}
    <div>{store.counter}</div>

    {/* actions are available */}
    <button onClick={store.inc}>+</button>
    <button onClick={store.dec}>-</button>
  </div>
);

// inject stores by their keys as defined in `<Provider />` to have them as render props
<Subscriber store anotherStore ..>
  {(store, anotherStore, ..) => <Counter {...{ store, anotherStore, .. }} />}
</Subscriber>

// or through a higher order component, a thin wrapper around `<Subscriber />`
subscribe('store', 'anotherStore', ..)(Counter);

The app

import { Provider } from 'qaf';

// the prop name is the key of the store used earlier in `<Subscriber />`
<Provider store={Store} anotherStore={AnotherStore} ..>
  <Counter />
</Provider>

Advanced

The container

<Provider />, <Subscriber /> and subscribe() are all components of a container, which is a collection of store instances. By default, Qaf exposes a main container that we can immediately put to use, but what if we wanted more than one container? (e.g. we need multiple instances of a single store).

import { createContainer } from 'qaf';

// this creates a container with context components, we should do it uniquely for every container
const QafContainer = createContainer();

// which exposes the following components
<QafContainer.Provider .. />
<QafContainer.Subscriber .. />

// and the following method
QafContainer.subscribe(..);

The singular container

Singular containers are a stripped-down version of Qaf containers, where the app uses one single store that houses all the shared state, actions and lifecycle methods. Singular containers are more performant, they utilize one context instead of having multiple contexts (one for each store) and require less computations (no internal component composition and nesting). However, -and as implied in the terminology- we are limited to the usage of one store only.

  • The app has one store, which is just a React component.
  • The store acts as a provider that exposes its instance to its subscribers.
  • Any component can subscribe to the store instance provided.
import { createContainer } from 'qaf';

// this creates a singular container with context components, this is only done once
const { SingularStore, Subscriber, subscribe } = createContainer({
  singular: true
});

// no invocation here, direct inheritance
class Store extends SingularStore {
  state = {
    counter: 0,

    inc: () => this.setState(state => ({ counter: state.counter + 1 })),
    dec: () => this.setState(state => ({ counter: state.counter - 1 }))
  };
}

// no need to pass any keys
<Subscriber>{store => <Counter {...{ store }} />}</Subscriber>;

// or with a HOC
subscribe(Counter);

// one provider for the whole app
<Store>
  <Counter />
</Store>;

Testing

Qaf stores are React components, we would test them as we would test any other component (Enzyme example).

Examples

Available here (source).

Edit qaf