Lädt...


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


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Let's dive into React State Management with Jotai.

Passing down values and dispatchers from a useState hook through props is a dreadful 🤢 process. You can avoid all that with two simple hooks 🪝.

useState + Props

No state management ⚰️

import { useState } from 'react';

const Component = () => {
  const [value, setValue] = useState("");

  return <div>
    <input value={value} setValue={(e) => setValue(e.target.value)} />
    <OtherComponent value={value}/>
  </div>
}

const OtherComponent = ({ value }) => {
  return <div>Value is: {value}</div>
}

Jotai atoms

Like a global useState 🤘

import { atom, useAtom } from 'jotai';

const valueAtom = atom("")

const Component = () => {
  const [value, setValue] = useAtom(valueAtom);

  return <div>
    <input value={value} setValue={(e) => setValue(e.target.value)} />
    <OtherComponent />
  </div>
}

const OtherComponent = () => {
  const [value] = useAtom(valueAtom);

  return <div>Value is: {value}</div>
}

That's it ✨! You can even export atoms from a file and access them in different files.

LocalStorage + States

Saving state values to localStorage is an absolute pain 🗡 in React. Jotai's atomWithStorage hook is an atom with localStorage binding ⛓.

atomWithStorage takes two parameters:

  1. localStorage key name 🔑
  2. Initial value
import { useAtom } from 'jotai';
import { atomWithStorage } from 'jotai/utils'

// "inputValue" is the localStorage key, "" is the initial value
const valueAtom = atomWithStorage("inputValue", "")

const Component = () => {
  const [value, setValue] = useAtom(valueAtom);

  return <div>
    <input value={value} setValue={(e) => setValue(e.target.value)} />
  </div>
}

That's all there is to it ⚡️!

Thanks for reading! If you have any feedback or comments, let me know!

If you enjoy my content, follow me at @IroncladDev on twitter for more!

Say Hello 👋

🐱 Github⠕ Replit🌐 Website✉️ Email

...

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


📈 69.17 Punkte
🔧 Programmierung

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


📈 38.12 Punkte
🔧 Programmierung

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


📈 34.12 Punkte
🔧 Programmierung

🔧 JOTAI(GLOBAL STATE MANGEMENT)


📈 34.12 Punkte
🔧 Programmierung

🔧 State Scope with Providers - Next.js with Jotai


📈 34.12 Punkte
🔧 Programmierung

🐧 PSA: I reduced my boot time from ~30 seconds to ~3 seconds by editing a single file (Linux Mint 19.1)


📈 27.5 Punkte
🐧 Linux Tipps

📰 Hackers Pwned Apple Safari in seconds; Google Pixel in 60 seconds


📈 27.5 Punkte
📰 IT Security

📰 Hackers Pwned Apple Safari in seconds; Google Pixel in 60 seconds


📈 27.5 Punkte
📰 IT Security

🔧 Implementing Dark Mode with React, Jotai, and Custom Hooks


📈 26.72 Punkte
🔧 Programmierung

🔧 How Jotai Was Born


📈 26.72 Punkte
🔧 Programmierung

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


📈 26.72 Punkte
🔧 Programmierung

🔧 Jotai atomWithStorage


📈 26.72 Punkte
🔧 Programmierung

🔧 Jotai Tips


📈 26.72 Punkte
🔧 Programmierung

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


📈 26.72 Punkte
🔧 Programmierung

🔧 Why I love Jotai


📈 26.72 Punkte
🔧 Programmierung

🔧 Why useSyncExternalStore Is Not Used in Jotai


📈 26.72 Punkte
🔧 Programmierung

🔧 State Management in Reactjs: A Guide to Choosing the Right State Management Tool for Your Projects


📈 22.79 Punkte
🔧 Programmierung

🔧 Object-Based UI State Management vs. JavaScript Proxy-Based UI State Management


📈 22.79 Punkte
🔧 Programmierung

🔧 React State Management Toolkit: Simplifying State Management


📈 22.79 Punkte
🔧 Programmierung

🔧 How to create a proper Event Management System SASS?


📈 21.29 Punkte
🔧 Programmierung

📰 Introduction To Proper Task Management


📈 21.29 Punkte
📰 IT Security Nachrichten

📰 Introduction To Proper Task Management


📈 21.29 Punkte
📰 IT Security Nachrichten

⚠️ Proper Amiga Productions Release Management


📈 21.29 Punkte
⚠️ Malware / Trojaner / Viren

📰 7 Steps For Proper Patch Management Process


📈 21.29 Punkte
📰 IT Security Nachrichten

🔧 🛠️ Pro Tip: Implementing IDisposable for Proper Resource Management


📈 21.29 Punkte
🔧 Programmierung

🪟 Xbox Series X load tests have State of Decay 2 load in seven seconds


📈 21.15 Punkte
🪟 Windows Tipps

🔧 State Management in React Using MobX State Tree (MST)


📈 18.8 Punkte
🔧 Programmierung

🔧 Master React Global State with a Custom Hook: Simplify Your App’s State Management


📈 18.8 Punkte
🔧 Programmierung

🔧 State Management in React – When and Where to use State


📈 18.8 Punkte
🔧 Programmierung

🔧 Simple State Management in JavaScript with Nanny State


📈 18.8 Punkte
🔧 Programmierung

🕵️ Apple macOS up to 10.15.0 State Management state issue


📈 18.8 Punkte
🕵️ Sicherheitslücken

🐧 A bash syntax highlighter that encourages (and can fix) proper quoting of variables


📈 17.3 Punkte
🐧 Linux Tipps

📰 Department of Telecom issues advisory to public on proper use of wireless jammer, booster


📈 17.3 Punkte
📰 Alle Kategorien

🐧 Reboot and select proper boot device or Insert boot media in selected boot device and press a key


📈 17.3 Punkte
🐧 Linux Tipps

matomo