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

stored-component

v0.1.0

Published

Keep a Preact component's state elsewhere for when you'll want it later.

Downloads

2

Readme

Stored Components

Keep a Preact component's state elsewhere for when you'll want it later.

Installation

npm install -S stored-component

Usage

Basic Usage

Instead of extending from Component use StoredComponent from the package. In addition, pass the default prop values to super().

import { h } from "preact";
import StoredComponent from "stored-component";

class MyComponent extends StoredComponent {
  constructor(props) {
    super(props, {foo: "bar", enabled: false});
  }

  // ...
}

Because the StoredComponent class uses the componentDidUpdate() callback to keep the data store in sync, if your component includes an implementation of componentDidUpdate() you'll need to call super.componentDidUpdate():

class MyComponent extends StoredComponent {
  constructor(props) {
    super(props, {foo: "bar", enabled: false});
  }

  componentDidUpdate() {
    console.log("Updated!");
    super.componentDidUpdate();
  }
}

Per-Instance Storage

By default a StoredComponent will associate its stored state with its class name. Without changing this behavior all MyComponents as defined above would share the same state. This can be overridden by passing in an id parameter to the StoredComponent constructor:

class MyComponent extends StoredComponent {
  constructor(props) {
    super(props, {foo: "bar", enabled: false}, props.id);
  }

  // ...
}

Typing

This project supports TypeScript. You can set the state and prop types of your StoredComponents just as with Preact Components:

type StateType = typeof defaultState;
const defaultState = {foo: "bar", enabled: false};

class MyComponent extends StoredComponent<{}, StateType> {
  constructor(props) {
    super(props, defaultState);
  }

  // ...
}

Recalling the Original State

You can reset a component's state to its default with the defaultState() method on the StoredComponent class.

Using a Different Data Store

The default data store used is window.sessionStorage. Other storage implementations can be set using the setStore() function.

import { storage } from "stored-component";
storage.setStore(window.localStorage);

Take note that if you use localStorage you will need to be aware of state model changes. If your application is updated with a new state structure, the old state still sitting in localStorage will be loaded up anyway.

Serialization

Because localStorage and sessionStorage can only store strings, the component states are serialized as JSON. This means that values in the state that can not be serialized as JSON are filtered out.

When recalling a filtered value from the data store, the default value (as defined when calling super()) is used.

For example - if you try to serialize this state:

const default = {foo: "", baz: -1, data: null};
let state = {
  foo: "bar",
  baz: 0,
  data: new Promise(...)
};

The following JSON will be stored:

{"foo": "bar", "baz": 0}

And the following object will be recalled from the store:

let state = {
  foo: "bar",
  baz: 0,
  data: null
}

In addition, all Arrays have non-serializable values filtered out.