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-0

v1.2.5

Published

Type-safe React application state library with zero setup

Downloads

55

Readme

use-0 exports the getUse function that turns any object into a use-able object whose properties are listened to by the use property hook inside React components.

// appState.ts
import { getUse } from 'use-0';

class AppState {
  // define "use"
  readonly use = getUse<AppState>();

  // define other properties
  count = 0;
  ids: number[] = [];

  // define methods
  readonly increment = () => this.count++;
  readonly decrement = () => this.count--;
}

// instantiate the class and export the instance
const appState = new AppState();

export default appState;
// MyComponent.ts
import appState from './appState';

const MyComponent = () => {
  // the component is re-rendered when 'count' or 'ids' are reassigned
  const count = appState.use('count');
  const ids = appState.use('ids');

  return (
    <div onClick={() => appState.count++}>Clicks: {count}</div>
  )
}

export default MyComponent;

A use-able object is a regular object, and all its properties and methods are used in the traditional way. Properties need to maintain immutability to invoke the property accessor. The readonly prefix protects properties from being reassigned.

appState.count++;
appState.ids = [...store.users.ids, 4];
appState.increment();
appState.increment = () => {}; // error because of the "readonly" prefix

Other ways to invoke

Plain object

// appState.ts
import { type WithUse, getUse } from 'use-0';

type AppState = WithUse<{
  count: number;
  ids: number[];
}>;

const appState: AppState = {
  use: getUse(),
  count: 0,
  ids: [],
}

Static class

Use the same principle as with an instantiable class, but all class members are defined with the static prefix, and the class isn't instantiated but used directly as an object.

// appState.ts
import { getUse } from 'use-0';

export default class AppState {
  // define "use"
  static readonly use = getUse<typeof AppState>();

  // define other properties
  static count = 0;
  static ids: number[] = [];
  
  // define methods
  static readonly increment = () => this.count++;
  static readonly decrement = () => this.count--;
}
// MyComponent.ts
import AppState from './appState';

const MyComponent = () => {
  const count = AppState.use('count');
  const ids = AppState.use('ids');

  return (
    <div onClick={() => AppState.count++}>Clicks: {count}</div>
  )
}

export default MyComponent;

State Structure

It's recommended to split the application state into files and then collect them into a centralized application state object. In the example below, we create two classes that are instantiated in the AppState class. They also implement a constructor that accepts the appState as an argument and defines the #appState property, which can be used by other methods. This is just one of the many ways to build the app state; feel free to adapt it.

// PostState.ts
import { getUse } from 'use-0';
import type { AppState } from './appState';

class PostState {
  readonly use = getUse<PostState>();
  
  ids: number[] = [];
  
  readonly loadPosts = async () => await fetch(/* ... */);

  readonly #appState: AppState;

  constructor(appState: AppState) {
    this.#appState = appState;
  }
}
// CommentState.ts
import { getUse } from 'use-0';
import type { AppState } from './appState';

class CommentState {
  readonly use = getUse<CommentState>();
  
  ids: number[] = [];
  
  readonly loadComments = async () => await fetch(/* ... */);

  readonly #appState: AppState;

  constructor(appState: AppState) {
    this.#appState = appState;
  }
}
// appState.ts
import { getUse } from 'use-0';
import PostState from './PostState';
import CommentState from './CommentState';

class AppState {
  readonly use = getUse<AppState>();

  readonly post: PostState;
  readonly comment: CommentState;

  constructor() {
    this.post = new PostState(this);
    this.comment = new CommentState(this);
  }
}

// instantiate the class and export the instance
const appState = new AppState();

export type { AppState };

export default appState;

Components can import appState and access other use-able objects as members of AppState.

// MyComponent.ts
// ...
useEffect(() => {
  void appState.post.loadPosts().then((ids) => {
    appState.post.ids = ids;
  });
})
// ...

I hope you'll find it useful in your next project!