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

@passionware/react-fast-context

v1.0.1-rc14

Published

Minimal, efficient and context based state management for React

Downloads

47

Readme

@passionware/react-fast-context

Minimal, efficient and context based state consumption for React

NPM JavaScript Style Guide

This library is a small addition to react context state management. The aim of react-fast-context if to expose simple API that is similar to just useState() and useContext() but keeping the performance of context consumers at the same level as react-redux does.

react-fast-context is like react-redux but without redux.

Install

npm install --save react-fast-context

Usage

How to create store

You can use createStore() to create new store instance, or useStore() hook to create and access it directly in the component.

Once you have store instance, simply pass it to react context using StoreProvider.

import { useState } from 'react';
import { useStore, StoreProvider } from '@passionware/react-fast-context';
import { useMatch } from '@reach/router';

const MyApp = () => {
  const [todos, setTodos] = useState([]);
  const [counter, setCounter] = useState(0);
  const selectedTab = useMatch('/:tab').tab;

  const store = useStore({ todos, counter, selectedTab });

  return <StoreProvider value={store}>your application</StoreProvider>;
};

Accessing the store

You can efficiently bind any component to any part of state by using selector functions, exactly the same as you do it in react-redux. useSelector(selector) hook will return fresh result of selector. It also triggers re-render of the component every time store is updated and selector value changes.

import { useSelector } from '@passionware/react-fast-context';

const selectTodo = (state, id) => state.todos.find(todo => todo.id === id);

const TodoViewer = ({ id }) => {
  const selectCurrentTodo = useCallback(state => selectTodo(state, id), [id]);
  const item = useSelector(selectCurrentTodo);

  return <li>I am item {item.label}</li>;
};

Updating the state

react-fast-context isn't opinionated about how to perform modifications. The library focuses on effective state share and consumption through React context.

In contrast to redux, when you have to imperatively dispatch an action to mutate the state, here you don't directly update any data, you describe state in a declarative way, the same as you would do it when simply sharing state value via context. To do so, you just need to pass up-to-date state to useStore() hook.

Thanks to that you can accept state portions from component props or hooks and send them into application context to be consumed effectively.

Please note that updating the state in such way causes a lot of re-renders of main component.
You need to use `React.memo` on its children in order to block this re-rendering.
Nested components that subscribe to store will re-render only when selected value changes.

Recommended way

Although react-fast-context is only about sharing the state, it plays nice with different strategies of handling updates.

However, it is recommended to share update API also via react context. Update API contains business-level functions that update original data that are composed into store state.

Example with local state and routing as a source:

import { useState } from 'react';
import { useStore, StoreProvider } from '@passionware/react-fast-context';
import { useMatch, useLocation } from '@reach/router';
import { ApiContext } from './api';

const MyApp = () => {
  const [todos, setTodos] = useState([]);
  const [counter, setCounter] = useState(0);
  const selectedTab = useMatch('/:tab').tab;
  const location = useLocation();

  const store = useStore({ todos, counter, selectedTab });
  const api = useMemo(
    () => ({
      addTodo: todo => setTodos(todos => [...todos, todo]),
      increment: () => setCounter(counter => counter + 1),
      navigate: tab => location.push('/' + tab),
    }),
    [setTodos, setCounter]
  );

  return (
    <StoreProvider value={store}>
      <ApiContext.Provider value={api}>your application</ApiContext.Provider>
    </StoreProvider>
  );
};

After you share update API via context, you can easily consume it in any component:

const AddTodoButton = () => {
  const { addTodo } = useContext(ApiContext);
  return <button onClick={() => addTodo({ name: 'new todo' })}>Add todo</button>;
};

API

createStore

It accepts initial store state and returns instance of a new store.

  • store.replace() (internal) replaces state with new one and notifies all subscribed components. You don't need to use it directly.
  • store.getState() (internal) returns current state value. You don't need to use it directly.
  • store.subscribe() (internal) is used by useSelector to listen store changes. You don't need to use it directly.

useStore

This hook creates the store at first component render and keeps the reference for future renders. This is just a convenient helper to not use createStore directly in a component.

If the component is updated and the hook receives different state as an argument, the store will be automatically updated with that value. This is very useful when you don't want imperatively update the state but rather build the state in a declarative way, deriving other state.

import { useStore } from '@passionware/react-fast-context';

const MyApp = () => {
  const store = useStore(currentState);
};

useSelector

useSelector(selector, compareFunction);

This hook returns a selector result performed on always recent store value. You have to remember not to re-create the selection function every render. You should create selector outside component or use useCallback if a selector depends on component state / props.

There is an optional compareFunction if your selector computes derived data causing new value not being referentially equal. You can read more about this in react-redux docs.

useStoreContext

A hook to return store from a context. You don't need to use it directtly.

License

MIT © adamborowski