Proper State Management πŸ›  with Jotai πŸ‘» in 30 seconds πŸ•– πŸ”₯

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 ⚰️


This content originally appeared on DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» and was authored by IroncladDev

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


This content originally appeared on DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» and was authored by IroncladDev


Print Share Comment Cite Upload Translate Updates
APA

IroncladDev | Sciencx (2023-02-09T13:42:24+00:00) Proper State Management πŸ›  with Jotai πŸ‘» in 30 seconds πŸ•– πŸ”₯. Retrieved from https://www.scien.cx/2023/02/09/proper-state-management-%f0%9f%9b%a0-with-jotai-%f0%9f%91%bb-in-30-seconds-%f0%9f%95%96-%f0%9f%94%a5/

MLA
" » Proper State Management πŸ›  with Jotai πŸ‘» in 30 seconds πŸ•– πŸ”₯." IroncladDev | Sciencx - Thursday February 9, 2023, https://www.scien.cx/2023/02/09/proper-state-management-%f0%9f%9b%a0-with-jotai-%f0%9f%91%bb-in-30-seconds-%f0%9f%95%96-%f0%9f%94%a5/
HARVARD
IroncladDev | Sciencx Thursday February 9, 2023 » Proper State Management πŸ›  with Jotai πŸ‘» in 30 seconds πŸ•– πŸ”₯., viewed ,<https://www.scien.cx/2023/02/09/proper-state-management-%f0%9f%9b%a0-with-jotai-%f0%9f%91%bb-in-30-seconds-%f0%9f%95%96-%f0%9f%94%a5/>
VANCOUVER
IroncladDev | Sciencx - » Proper State Management πŸ›  with Jotai πŸ‘» in 30 seconds πŸ•– πŸ”₯. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/09/proper-state-management-%f0%9f%9b%a0-with-jotai-%f0%9f%91%bb-in-30-seconds-%f0%9f%95%96-%f0%9f%94%a5/
CHICAGO
" » Proper State Management πŸ›  with Jotai πŸ‘» in 30 seconds πŸ•– πŸ”₯." IroncladDev | Sciencx - Accessed . https://www.scien.cx/2023/02/09/proper-state-management-%f0%9f%9b%a0-with-jotai-%f0%9f%91%bb-in-30-seconds-%f0%9f%95%96-%f0%9f%94%a5/
IEEE
" » Proper State Management πŸ›  with Jotai πŸ‘» in 30 seconds πŸ•– πŸ”₯." IroncladDev | Sciencx [Online]. Available: https://www.scien.cx/2023/02/09/proper-state-management-%f0%9f%9b%a0-with-jotai-%f0%9f%91%bb-in-30-seconds-%f0%9f%95%96-%f0%9f%94%a5/. [Accessed: ]
rf:citation
» Proper State Management πŸ›  with Jotai πŸ‘» in 30 seconds πŸ•– πŸ”₯ | IroncladDev | Sciencx | https://www.scien.cx/2023/02/09/proper-state-management-%f0%9f%9b%a0-with-jotai-%f0%9f%91%bb-in-30-seconds-%f0%9f%95%96-%f0%9f%94%a5/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.