[React] TIL 0516 - Local Storage, useReducer

์ œ๋ก ยท2022๋…„ 5์›” 16์ผ
0

#React

๋ชฉ๋ก ๋ณด๊ธฐ
1/11
post-thumbnail
  • ๐Ÿ’‘ย ์ข‹์•˜๋˜ ๊ฒƒ(Liked)
    • ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€๋ž€ ๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ๊ณ  ํ™œ์šฉํ•ด ๋ณธ ๊ฒƒ!
  • ๐ŸŒŸย ๋ฐฐ์šด ๊ฒƒ(Learned)
    • ๋ถ€๋ชจ โ†’ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ref๋ฅผ ๋„˜๊ฒจ์ค„ ๋•Œ๋Š” 1๊ฐœ ๋ฐ–์— ๋‚ด๋ ค์ฃผ์ง€ ๋ชปํ•จ

    • ๊ทธ๋ž˜์„œ forwardRef๋กœ ๋ฌถ์–ด์„œ ๋ณด๋‚ด์ค€๋‹ค.

    • ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค.

    • Props ๋„˜๊ธธ ๋•Œ ๊ผญ! ์ค‘๊ด„ํ˜ธ ์•ˆ์œผ๋กœ ๋„˜๊ธฐ๊ธฐ!!

      <Button>
        <Button>text</Button>
      </Button>
      which is not good. Because valid html does not support buttons within buttons... so what you want is most likely to be something like:
      
      <div>
        <Button>text</Button>
      </div>

      LocalStorage์— ๋ฐ์ดํ„ฐ ์ €์žฅ

    • ๋ฐ์ดํ„ฐ๋ฅผ ์ž„์‹œ๋กœ ์›น ๋ธŒ๋ผ์šฐ์ €์— ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ, ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž์—ด๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ

    • ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ๋Š” JSON.stringify()๋กœ jsonํ™” ์‹œ์ผœ์ฃผ๊ณ ,

    • ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋Š” JSON.parse()๋กœ ๊ฐ์ฒดํ™” ์‹œ์ผœ์ค˜์•ผํ•จ!

      • LocalStorage โ†’ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ๋‹ซ์•„๋„ ์ €์žฅ๋จ
      • SessionStorage โ†’ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ๋‹ซ์œผ๋ฉด ์—†์–ด์ง
    • ๋ฉ”์„œ๋“œ
      - .getItem() โ†’ ๋กœ์ปฌ์Šคํ† ๋ฆฌ์ง€์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ด
      - .setItem() โ†’ ๋กœ์ปฌ์Šคํ† ๋ฆฌ์ง€์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ

      ๋ฐ์ดํ„ฐ ์ €์žฅํ•˜๊ธฐ

      localStorage.setItem(โ€˜Keyโ€™,โ€™Valueโ€™)

    • JSON.parse โ†’ json ํ˜•ํƒœ์˜ ํŒŒ์ผ์„ object ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•ด์คŒ

    • JSON.stringify โ†’ object ํ˜•ํƒœ์˜ ํŒŒ์ผ์„ json ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•ด์คŒ

profile
Software Developer

0๊ฐœ์˜ ๋Œ“๊ธ€