Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
JotaiX is a custom extension of Jotai, a primitive and flexible state management library for React. Jotai offers a minimalistic API to manage global, derived, or async states in React, solving common issues such as unnecessary re-renders or complex context management. JotaiX builds upon this foundation, providing enhanced utilities and patterns for more efficient and streamlined state management in larger and more complex applications.
jotai-x
, built on top of jotai
, is providing a powerful store factory
which solves these challenges, so you can focus on your app.
yarn add jotai jotai-x
For further details and API documentation, visit jotai-x.udecode.dev.
jotai-x
?JotaiX allows for the creation of structured stores with ease, integrating seamlessly with Jotai's atom concept.
import { createAtomStore } from 'jotai-x';
// Notice how it uses the name of the store in the returned object.
export const { useElementStore, ElementProvider } = createAtomStore({
element: null
}, {
name: 'element'
});
The createAtomStore
function simplifies the process of creating and managing atom-based states.
createAtomStore<T extends object>(initialState: T, options?: CreateAtomStoreOptions): AtomStoreApi;
initialState
: This is an object representing the initial state of your store. Each key-value pair in this object is used to create an individual atom. This is required even if you want to set the initial value from the provider, otherwise the atom would not be created.options
: Optional. This parameter allows you to pass additional configuration options for the store creation.The options
object can include several properties to customize the behavior of your store:
name
: A string representing the name of the store, which can be helpful for debugging or when working with multiple stores.delay
: If you need to introduce a delay in state updates, you can specify it here. Optional.effect
: A React component that can be used to run effects inside the provider. Optional.extend
: Extend the store with derived atoms based on the store state. Optional.The createAtomStore
function returns an object (AtomStoreApi
) containing the following properties and methods for interacting with the store:
use<Name>Store
:
get
, set
, use
and store
, where values are hooks for each state defined in the store.get
: Hooks for accessing a state within a component, ensuring re-rendering when the state changes. See useAtomValue.set
: Hooks for setting a state within a component. See useSetAtom.use
: Hooks for accessing and setting a state within a component, ensuring re-rendering when the state changes. See useAtom.store
: A hook to access the JotaiStore for the current context.const [element, setElement] = useElementStore().use.element()
<Name>Provider
:
<name>Store
:
atom
: Access the atoms used by the store, including derived atoms defined using extend
. See atom.createAtomStore
generates a provider component (<Name>Provider
) for a Jotai store. This provider not only supplies the store to its child components but also handles hydrating and syncing the store's state. Here's how it works:
initialValues
prop.<state>
props: there is one for each state defined in the store.JotaiX creates scoped providers, enabling more granular control over different segments of state within your application. createAtomStore
sets up a context for each store, which can be scoped using the scope
prop. This is particularly beneficial in complex applications where nested providers are needed.
There are two ways of creating derived atoms from your JotaiX store.
extend
Atoms defined using the extend
option are made available in the same places as other values in the store.
const { useUserStore } = createAtomStore({
username: 'Alice',
}, {
name: 'user',
extend: (atoms) => ({
intro: atom((get) => `My name is ${get(atoms.username)}`),
}),
});
const intro = useAppStore().get.intro();
Derived atoms can also be defined externally by accessing the store's atoms through the <name>Store
API. Externally defined atoms can be accessed through the store using the special use<Name>Store().{get,set,use}.atom
hooks.
const { userStore, useUserStore } = createAtomStore({
username: 'Alice',
}, { name: 'user' });
const introAtom = atom((get) => `My name is ${get(userStore.atom.username)}`);
const intro = useUserStore().get.atom(introAtom);
import { createAtomStore } from 'jotai-x';
export type AppStore = {
name: string;
onUpdateName: (name: string) => void;
};
const initialState: Nullable<AppStore> = {
name: null,
onUpdateName: null,
};
export const { useAppStore, AppProvider } = createAtomStore(
initialState as AppStore,
{ name: 'app' }
);
// ...
const App = () => {
return (
<AppProvider
initialValues={{
onUpdateName: (name: string) => console.log(name)
}}
// Either here or in initialValues
name="John Doe"
>
<Component />
</AppProvider>
);
};
const Component = () => {
const [name, setName] = useAppStore().use.name();
const onUpdateName = useAppStore().get.onUpdateName();
return (
<div>
<input value={name} onChange={(e) => setName(e.target.value)} />
<button onClick={() => onUpdateName(name)}>Update</button>
</div>
);
};
const App = () => {
return (
// Parent scope
<AppProvider
scope="parent"
initialValues={{
onUpdateName: (name: string) => console.log("Parent:", name)
}}
name="Parent User"
>
<div>
<h1>Parent Component</h1>
<Component />
{/* Child scope */}
<AppProvider
scope="child"
initialValues={{
onUpdateName: (name: string) => console.log("Child:", name)
}}
name="Child User"
>
<div>
<h2>Child Component</h2>
<Component />
</div>
</AppProvider>
</div>
</AppProvider>
);
};
// Accessing state from the specified scope.
const Component = () => {
// Here, we get the state from the parent scope
const [name, setName] = useAppStore('parent').use.name();
// Here, we get the state from the closest scope (default)
const onUpdateName = useAppStore().get.onUpdateName();
return (
<div>
<input value={name || ''} onChange={(e) => setName(e.target.value)} />
<button onClick={() => onUpdateName(name)}>Update Name</button>
</div>
);
};
Discussions is the best place for bringing opinions and contributions. Letting us know if we're going in the right or wrong direction is great feedback and will be much appreciated!
🌟 Stars and 📥 Pull requests are welcome! Don't hesitate to share your feedback here. Read our contributing guide to get started.
FAQs
Jotai store factory for a best-in-class developer experience.
The npm package jotai-x receives a total of 50,733 weekly downloads. As such, jotai-x popularity was classified as popular.
We found that jotai-x demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.