Lädt...


🔧 jotai-wrapper, a super tiny and simple utility library


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

I've just published jotai-wrapper, a super tiny and simple utility library that makes using jotai even simpler. It comes from the necessity to migrate a project with react-context-slices to jotai. Both libraries share a similar API. While in react-context-slices you have the useSlice hook, in jotai you have the useAtom, useSetAtom, and useAtomValue hooks. In react-context-slices you define either React Context or Redux slices, while in jotai you define atoms. The need for the migration from the first to the second was a high memory usage by react-context-slices when using React Context slices.

The API

jotai-wrapper default exports a function, getAPIFromAtoms. To use it you must define an atoms.js file in your project, like this:

// atoms.js
import { atom } from "jotai";
import getAPIFromAtoms from "jotai-wrapper";

export const { useAtom, useSetAtom, useAtomValue, getAtom, selectAtom } =
  getAPIFromAtoms({
    counter: atom(0),
    todos: atom([]),
    messagesLastFoundId: atom(-1),
    invitationsLastFoundId: atom(-1),
    // rest of atoms
  });

As you can see this function, getAPIFromAtoms, gives us three hooks and two functions.

useAtom, useSetAtom, and useAtomValue

These three hooks behave the same as in jotai, but you pass to them a string instead of an atom (you can still pass them an atom if you wish). For example:

// counter.js
import {useAtom} from "./atoms";

export default function Counter(){
    const [counter,setCounter]=useAtom("counter");

    return <>
    <button onClick={()=>setCounter(c=>c+1)}>+</button>{counter}
    <>;
}

The fact to use strings instead of atoms in the this three hooks allows for dynamic referencing, like this:

// use-last-found-id.js
import { useSetAtom } from "./atoms";

export function useLastFountId({ prefix }) {
  const setLastFoundId = useSetAtom(`${prefix}LastFoundId`);
  // ...
}

getAtom and selectAtom

These are the other two functions returned by the call to getAPIFromAtoms in atoms.js file.

getAtom simply returns the atom wich the string we pass refers to. For example: const counterAtom = getAtom("counter");.

selectAtom is more useful. We use it like this:

// todos.js
import { selectAtom, useAtomValue } from "./atoms";
import { useMemo } from "react";

export default function Todos({ index, id }) {
  const todoAtIndex = useAtomValue(
    useMemo(() => selectAtom("todos", (todos) => todos[index]), [index])
  );
  const todoWithId = useAtomValue(
    useMemo(
      () =>
        selectAtom("todos", (todos) => todos.find((todo) => todo.id === id)),
      [id]
    )
  );
  //...
}

It's important to note that this selectAtom function returned by the call to getAPIFromAtoms in atoms.js file is different than the selectAtom from jotai. To use the selectAtom from jotai you must import it like this: import {selectAtom} from "jotai/utils";.

Summary

So this is it. You can use jotai with jotai-wrapper in a simpler way. Thanks for reading.

...

⚠️ Making of Tiny Bobble, Tiny Galaga, Tiny Invaders & Tinyus


📈 37.98 Punkte
⚠️ Malware / Trojaner / Viren

🐧 [utility] bkp - simple utility for creating simple backups


📈 37.33 Punkte
🐧 Linux Tipps

🐧 [utility] bkp - simple utility for creating simple backups


📈 37.33 Punkte
🐧 Linux Tipps

🔧 What is Utility Analysis?|Total Utility and Marginal Utility


📈 34.91 Punkte
🔧 Programmierung

🔧 Best and Worst Situations to Use Zustand and Jotai with Next.js


📈 30.42 Punkte
🔧 Programmierung

🔧 Advanced State Management: Comparing Recoil, Zustand, and Jotai


📈 28.76 Punkte
🔧 Programmierung

🔧 Proper State Management 🛠 with Jotai 👻 in 30 seconds 🕖 🔥


📈 27.09 Punkte
🔧 Programmierung

🔧 Why useSyncExternalStore Is Not Used in Jotai


📈 27.09 Punkte
🔧 Programmierung

🔧 Why I love Jotai


📈 27.09 Punkte
🔧 Programmierung

🔧 State Scope with Providers - Next.js with Jotai


📈 27.09 Punkte
🔧 Programmierung

🔧 Jotai Tips


📈 27.09 Punkte
🔧 Programmierung

🔧 JOTAI(GLOBAL STATE MANGEMENT)


📈 27.09 Punkte
🔧 Programmierung

🔧 “Mail” example in shadcn-ui/ui manages state using Jotai.


📈 27.09 Punkte
🔧 Programmierung

🔧 Jotai atomWithStorage


📈 27.09 Punkte
🔧 Programmierung

🔧 How Jotai Was Born


📈 27.09 Punkte
🔧 Programmierung

🐧 TIC-80 tiny computer: a FOSS 'fantasy computer' for making, playing and sharing tiny games


📈 26.99 Punkte
🐧 Linux Tipps

🐧 pdd v1.2 tiny date, time diff utility released with timer and stopwatch support!


📈 25.41 Punkte
🐧 Linux Tipps

⚠️ [webapps] - Tiny Tiny RSS - Blind SQL Injection


📈 25.32 Punkte
⚠️ PoC

⚠️ Tiny Tiny RSS Blind SQL Injection


📈 25.32 Punkte
⚠️ PoC

🪟 Tiny Tiny RSS für den eigenen Webserver einrichten


📈 25.32 Punkte
🪟 Windows Tipps

⚠️ [webapps] - Tiny Tiny RSS - Blind SQL Injection


📈 25.32 Punkte
⚠️ PoC

⚠️ Tiny Tiny RSS Blind SQL Injection


📈 25.32 Punkte
⚠️ PoC

🪟 Tiny Tiny RSS für den eigenen Webserver einrichten


📈 25.32 Punkte
🪟 Windows Tipps

📰 Thinkcentre Tiny P320: Lenovos Tiny-Serie wird zur Workstation für sechs Displays


📈 25.32 Punkte
📰 IT Nachrichten

📰 How To Host Your Own RSS System On Linux With Tiny Tiny RSS


📈 25.32 Punkte
🖥️ Betriebssysteme

🐧 How To Host Your Own RSS System On Linux With Tiny Tiny RSS


📈 25.32 Punkte
🐧 Linux Tipps

🕵️ Tiny Tiny RSS vor 829d478f Cross Site Scripting [CVE-2017-1000035]


📈 25.32 Punkte
🕵️ Sicherheitslücken

🕵️ Tiny Tiny RSS 17.4 public.php login SQL Injection


📈 25.32 Punkte
🕵️ Sicherheitslücken

🕵️ Medium CVE-2017-16896: Tt-rss Tiny tiny rss


📈 25.32 Punkte
🕵️ Sicherheitslücken

🕵️ Tiny Tiny RSS prior 829d478f cross site scripting [CVE-2017-1000035]


📈 25.32 Punkte
🕵️ Sicherheitslücken

🕵️ Tiny Tiny RSS 17.4 public.php login sql injection


📈 25.32 Punkte
🕵️ Sicherheitslücken

🕵️ Medium CVE-2020-7724: Tiny-conf project Tiny-conf


📈 25.32 Punkte
🕵️ Sicherheitslücken

🪟 The discounted Razer Huntsman Mini is a tiny keyboard with a tiny price


📈 25.32 Punkte
🪟 Windows Tipps

⚠️ [webapps] Tiny Tiny RSS - Remote Code Execution


📈 25.32 Punkte
⚠️ PoC

matomo