
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
jotai-molecules
Advanced tools
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.
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 frombunshi
, and they can interoperated and depend on each others. Version 1.2.0 ofjotai-molecules
is just a wrapper forbunshi
.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.
This module is published on NPM as jotai-molecules
npm i jotai-molecules
Note: Prefer bunshi to
jotai-molecules
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>("bob@example.com");
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={"sam@example.com"}>
<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>
);
};
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({}),
};
});
getMol
- depend on the value of another moleculegetScope
- depend on the value of a scopeUse 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={"sam@example.com"}>
<UserComponent />
</ScopeProvider>
);
After:
useMolecule(UserMolecule, { withScope: [UserScope, "sam@example.com"] });
Creates a reference for scopes, similar to React Context
import { createScope } from "jotai-molecules";
/**
* Scope for a user id
*/
export const UserScope = createScope<string>("bob@example.com");
initialValue
the default value for molecules that depend on this scopeProvides 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={"sam@example.com"}>
<UserComponent />
</ScopeProvider>
);
scope
the MoleculeScope
reference to providevalue
a new value for that scopeFAQs
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.
The npm package jotai-molecules receives a total of 1,001 weekly downloads. As such, jotai-molecules popularity was classified as popular.
We found that jotai-molecules demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 10 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.