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

jotai-molecules

v1.2.0

Published

A tiny, fast, dependency-free 1.18kb library for creating jotai atoms in a way that lets you lift state up, or push state down.

Downloads

5,360

Readme

jotai-molecules has been renamed to bunshi.

All new users of this module should use bunshi instead. Bunshi adds support for vue, react and vanilla javascript. Development and features additions will continue under the bunshi package.

Molecules in jotai-molecules version 1.2.0 are compatible with molecules from bunshi, and they can interoperated and depend on each others. Version 1.2.0 of jotai-molecules is just a wrapper for bunshi.

Documentation: bunshi.org

A tiny, fast, dependency-free 1.18kb library for creating jotai atoms in a way that lets you lift state up, or push state down. See Motivation for more details on why we created this library.

Installation

This module is published on NPM as jotai-molecules

npm i jotai-molecules

Note: Prefer bunshi to jotai-molecules

Usage

Molecules are a set of atoms that can be easily scoped globally or per context.

import React from "react";
import { atom, useAtom } from "jotai";
import {
  molecule,
  useMolecule,
  createScope,
  ScopeProvider,
} from "jotai-molecules";

const CompanyScope = createScope<string>("example.com");

const CompanyMolecule = molecule((_, getScope) => {
  const company = getScope(CompanyScope);
  const companyNameAtom = atom(company.toUpperCase());
  return {
    company,
    companyNameAtom,
  };
});

const UserScope = createScope<string>("[email protected]");

const UserMolecule = molecule((getMol, getScope) => {
  const userId = getScope(UserScope);
  const companyAtoms = getMol(CompanyMolecule);
  const userNameAtom = atom(userId + " name");
  const userCountryAtom = atom(userId + " country");
  const groupAtom = atom((get) => {
    return userId + " in " + get(companyAtoms.companyNameAtom);
  });
  return {
    userId,
    userCountryAtom,
    userNameAtom,
    groupAtom,
    company: companyAtoms.company,
  };
});

const App = () => (
  <ScopeProvider scope={UserScope} value={"[email protected]"}>
    <UserComponent />
  </ScopeProvider>
);

const UserComponent = () => {
  const userAtoms = useMolecule(UserMolecule);
  const [userName, setUserName] = useAtom(userAtoms.userNameAtom);

  return (
    <div>
      Hi, my name is {userName} <br />
      <input
        type="text"
        value={userName}
        onInput={(e) => setUserName((e.target as HTMLInputElement).value)}
      />
    </div>
  );
};

API

molecule

Create a molecule that can be dependent on other molecules, or dependent on scope.

import { molecule } from "jotai-molecules";

export const PageMolecule = molecule(() => {
  return {
    currentPage: atom("/"),
    currentParams: atom({}),
  };
});
  • Requires a getter function
    • getMol - depend on the value of another molecule
    • getScope - depend on the value of a scope

useMolecule

Use a molecule for the current scope. Will produce a different value depending on the React context it is run in.

import { useMolecule } from "jotai-molecules";
import { useSetAtom, useAtomValue } from "jotai";

export const PageComponent = () => {
  const pageAtoms = useMolecule(PageMolecule);

  const setParams = useSetAtom(pageAtoms.currentPage);
  const page = useAtomValue(pageAtoms.currentPage);

  return (
    <div>
      Page: {page}
      <br />
      <button onClick={() => setParams({ date: Date.now() })}>
        Set current time
      </button>
    </div>
  );
};

By default useMolecule will provide a molecule based off the implicit scope from context. You can override this behaviour by passing options to useMolecule.

  • withScope - will overide a scope value (ScopeTuple<unknown>)
  • withUniqueScope - will override a scope value with a new unique value (MoleculeScope<unknown>)
  • exclusiveScope - will override ALL scopes (ScopeTuple<unknown>)

Instead of a scope provider, you can use an explicit scope when using a molecule. This can simplify integrating jotai with other hooks-based libraries.

Before:

const App = () => (
  <ScopeProvider scope={UserScope} value={"[email protected]"}>
    <UserComponent />
  </ScopeProvider>
);

After:

useMolecule(UserMolecule, { withScope: [UserScope, "[email protected]"] });

createScope

Creates a reference for scopes, similar to React Context

import { createScope } from "jotai-molecules";

/**
 *  Scope for a user id
 */
export const UserScope = createScope<string>("[email protected]");
  • initialValue the default value for molecules that depend on this scope

ScopeProvider

Provides a new value for Scope, similar to React Context. This will create new molecules in the react tree that depend on it.

const App = () => (
  <ScopeProvider scope={UserScope} value={"[email protected]"}>
    <UserComponent />
  </ScopeProvider>
);
  • scope the MoleculeScope reference to provide
  • value a new value for that scope