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

@known-as-bmf/react-store

v4.0.6

Published

React hook to subscribe to a store from @known-as-bmf/store.

Downloads

29

Readme

@known-as-bmf/react-store

React hook to subscribe to a store from @known-as-bmf/store.

Installation

npm install --save @known-as-bmf/store @known-as-bmf/react-store

You also need react (>= 16.8) installed in your project.

Description

This library provides a react hook to subscribe to a store from @known-as-bmf/store.

Usage

Existing store

import React, { FunctionComponent } from 'react';
import { of, swap } from '@known-as-bmf/store';
import { useStore } from '@known-as-bmf/react-store';

const store = of('Hello world');
const change = () => swap(store, (s) => `${s}!`);

const MyComponent: FunctionComponent = () => {
  const [message] = useStore(store);

  return (
    <div>
      <h1>{message}</h1>
      <button onClick={change}>add !</button>
    </div>
  );
};

The result of the hook is the state itself at index 0, and a partially applied swap() function (store parameter already filled) at index 1.

import React, { FunctionComponent, useCallback } from 'react';
import { of } from '@known-as-bmf/store';
import { useStore } from '@known-as-bmf/react-store';

const store = of(0);

const MyComponent: FunctionComponent = () => {
  const [count, swap] = useStore(store);

  const inc = useCallback(() => swap((c) => c + 1), [swap]);

  return (
    <div>
      <h1>{count}</h1>
      <button onClick={inc}>increment</button>
    </div>
  );
};

You can also provide a selector to only get part of the state (more info here).

import React, { FunctionComponent } from 'react';
import { of } from '@known-as-bmf/store';
import { useStore } from '@known-as-bmf/react-store';

const store = of({
  preferences: { theme: 'dark', lang: 'fr' },
  lastOnline: '2020-02-21T18:22:33.343Z',
  someArray: [],
});

const MyComponent: FunctionComponent = () => {
  const [theme] = useStore(store, (s) => s.preferences.theme);

  return <h1 className={theme}>Hello world!</h1>;
};

New store

You can also create a new store from an initial value using useNewStore().

import React, { FunctionComponent } from 'react';
import { swap } from '@known-as-bmf/store';
import { useNewStore } from '@known-as-bmf/react-store';

const MyComponent: FunctionComponent = () => {
  const [message, setMessage, store] = useNewStore('initial');

  // the two functions bellow are identical

  const a = useCallback(() => {
    setMessage((s) => `${s}!`);
  }, [setMessage]);

  const b = useCallback(() => {
    swap(store, (s) => `${s}!`);
  }, [store]);

  return (
    <div>
      <h1>{message}</h1>
      <button onClick={a}>add !</button>
      <button onClick={b}>add !</button>
    </div>
  );
};

The result of the hook is the state itself at index 0, a partially applied swap() function (store parameter already filled) at index 1 and the store instance at index 2.

API

/**
 * Subscribe to a store and re-render when its state changes.
 * @param store The store to subscribe to.
 */
function useStore<S>(store: Store<S>): StoreHookResult<S, S>;
/**
 * Subscribe to part of a state of a store and re-render when it changes.
 * @param store The store to subscribe to.
 * @param selector The part of the state to subscribe to.
 */
function useStore<S, R>(
  store: Store<S>,
  selector: Selector<S, R>
): StoreHookResult<S, R>;
type StoreHookResult<S, R> = [R, PartialSwap<S>];
type PartialSwap<S> = (mutationFn: (current: S) => S) => void;
/**
 * Create and subscribe to a store and re-render when its state changes.
 * @param initialState The initial value for the created store.
 */
function useNewStore<S>(initialState: S): NewStoreHookResult<S, S>;
type NewStoreHookResult<S, R> = [R, PartialSwap<S>, Store<S>];