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

use-memory-value

v2.0.2

Published

Hooks for global state in memory and localstorage

Downloads

27

Readme

use-memory-value

Hooks for global state in memory and localstorage. It's like context, but only one state per value; allows you to subscribe and update values from far away.

Using Expo? Check out this implementation: expo-use-memory-value.

Installation

yarn add use-memory-value

It has the following peerDependencies, along with react:

yarn add localforage react-fast-compare
# assumes react is already installed

Usage

Start by creating a new MemoryValue or StoredMemoryValue. You can declare this in any file, make sure it's exported and importable from all the files you want to use the value.

import { MemoryValue } from 'use-memory-value';

interface State {
  foo: number;
  bar: string;
  baz?: boolean;
}

const INITIAL_STATE: State = {
  foo: 42,
  bar: 'yes',
};

export const MY_STATE = new MemoryValue<State>(INITIAL_STATE);

Then, where you want to use the value, import the MemoryValue and useMemoryValue:

import { useMemoryValue, useMutableMemoryValue } from 'use-memory-value';

import { MY_STATE } from '../path/to/state';

function ReadOnlyBar() {
  const state = useMemoryValue(MY_STATE);
  return <h1>foo: {state && state.foo}</h1>;
}

function CountingFoo() {
  const [state, updateState] = useMutableMemoryValue(MY_STATE);
  const increment = () =>
    updateState((prev) => ({ ...prev, foo: prev.foo + 1 }));

  return (
    <button type="button" onClick={increment}>
      Foo: {state.foo}
    </button>
  );
}

function ActivateBaz() {
  const [, updateState] = useMutableMemoryValue(MY_STATE);
  const activate = () => updateState((prev) => ({ ...prev, baz: true }));

  return (
    <button type="button" onClick={activate}>
      Activate
    </button>
  );
}

If the value should be persisted to (and initialized from) local storage, use StoredMemoryValue:

export const MY_STATE = new StoredMemoryValue<State>('local.key.name');

TypeScript warnings

It is important to use type and not interface when using this in conjunction with TypeScript. The reason for this is that interfaces are extendible and thus we can not safely say that the final resolved shape is serializable (JSON-compatible). types are fixed, and thus can be checked.

You want this because non-serializable fields would be lost during serialization/deserialization, and thus can cause run-time issues.

If you get errors that the type is not Serializable, make sure you're only using type and not interface.

Localforage configuration

You can set the instance yourself by importing

import { setLocalForageInstance } from 'expo-use-memory-value/storage.web';