Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jotai

Package Overview
Dependencies
Maintainers
2
Versions
180
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jotai

👻 Primitive and flexible state management for React

  • 2.10.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1M
increased by6.7%
Maintainers
2
Weekly downloads
 
Created

What is jotai?

Jotai is a state management library for React that focuses on atomic state management. It allows you to manage state in a more granular and efficient way by breaking down the state into smaller, independent atoms.

What are jotai's main functionalities?

Basic Atom

This feature allows you to create a basic atom and use it in a React component. The `atom` function creates a new atom with an initial state, and the `useAtom` hook allows you to read and update the atom's state.

import { atom, useAtom } from 'jotai';

const countAtom = atom(0);

function Counter() {
  const [count, setCount] = useAtom(countAtom);
  return (
    <div>
      <p>{count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

Derived Atom

This feature allows you to create a derived atom that depends on the state of another atom. The `doubledCountAtom` is derived from `countAtom` and always holds double the value of `countAtom`.

import { atom, useAtom } from 'jotai';

const countAtom = atom(0);
const doubledCountAtom = atom((get) => get(countAtom) * 2);

function DoubledCounter() {
  const [doubledCount] = useAtom(doubledCountAtom);
  return <p>{doubledCount}</p>;
}

Async Atom

This feature allows you to create an atom that handles asynchronous operations. The `fetchUserAtom` fetches user data from an API and the `User` component displays the user's name once the data is loaded.

import { atom, useAtom } from 'jotai';

const fetchUserAtom = atom(async () => {
  const response = await fetch('https://jsonplaceholder.typicode.com/users/1');
  return response.json();
});

function User() {
  const [user] = useAtom(fetchUserAtom);
  return user ? <p>{user.name}</p> : <p>Loading...</p>;
}

Other packages similar to jotai

Keywords

FAQs

Package last updated on 20 Nov 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc