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

jotai-optics

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jotai-optics

## Background It's easy to "combine" two atoms, but how do we split an atom?

  • 0.1.12
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
111K
decreased by-30.48%
Maintainers
1
Weekly downloads
 
Created
Source

jotai-optics

Background

It's easy to "combine" two atoms, but how do we split an atom?

The answer is optics.

This is based on this issue: https://github.com/pmndrs/jotai/issues/44

We've got Prisms, Lenses, Isos, Traversals and of course Equivalences.

Usage

import { atom  } from 'jotai'
import { focus } from 'jotai-optics'

const bigAtom = atom({ a: 0, b: 5 })
const focusAAtom = focus(bigAtom, (optic) => optic.prop('a'))
const focusBAtom = focus(bigAtom, (optic) => optic.prop('b'))

useAtomArrayFamily

For when you have something of type Atom<Array<TodoItem>> but you would like to have Array<Atom<TodoItem>>, so for example, say that your TaskItem component looks like this:

const TaskItem = ({ atom }: { atom: RWAtom<TodoItem> }) => {
  const [value, onChange] = useAtom(atom)
  const toggle = () => {
    onChange(value => {
      return { ...value, checked: !value.checked }
    })
  }
  return (
    <li data-testid={value.task}>
      {value.task}
      <input
        data-testid={`${value.task}-checkbox`}
        type="checkbox"
        checked={value.checked || false}
        onChange={toggle}
      />
    </li>
  )
}

but the parent, TaskList only has an Atom<Array<TodoItem>> in it, that's when useAtomArrayFamily is useful:

const TaskList = ({ todoItems }: { todoItems: RWAtom<Array<TodoItem>> }) => {
  const atoms = useAtomArrayFamily(todoItems)
  return (
    <>
      {atoms.map(([todoItem], index) => (
        <TaskItem key={index} atom={todoItem} />
      ))}
    </>
  )
}

FAQs

Package last updated on 04 Nov 2020

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