Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- ์ ๋ฆฌํธ๋ฆฌํธ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ํ์ด์ฌ
- html
- ํ์ฒ์ ๋ฆฌํธ๋ฆฌํธ
- ๋ฐฐ์์ ๋ฐฐ์
- ๊น๋ฏธ๊ฒฝ์๋งํ์์
- ์ค๋ธ์
- css
- ์ฑ
- ๊ฐ๋ฐ
- ๊ฐ์ดํ ์ข ๋ญ๊ฐ๋น
- ์ฝ๋ฉ
- ์๋ฐ
- ์ํ
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์นดํ๋๊ฐ
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ์ค๋ผํด
- Python
- database
- ์นํผ๋ธ๋ฆฌ์ฑ
- ๋ ์
- K๋ฐฐํฐ๋ฆฌ๋ ๋ณผ๋ฃจ์
- JavaScript
- Java
- ๋๊ฐ
- ํ๋ก๊ทธ๋๋ฐ
- ์ํ์ฃผ
- ๋ฐ์ํ
Archives
- Today
- Total
JiYoung Dev ๐ฅ
[React] session/local storage์ ๋ฐ์ดํฐ ์ ์ฅํ๊ธฐ (2023.06.05) ๋ณธ๋ฌธ
Study/React
[React] session/local storage์ ๋ฐ์ดํฐ ์ ์ฅํ๊ธฐ (2023.06.05)
Shinjio 2023. 6. 5. 11:57๋ฐ์ํ
Session/Local Storage์ ๋ฐ์ดํฐ ์ ์ฅํ๊ธฐ
/**
* Local Storage vs Session Storage
* - ์ฃผ์์ฌํญ! ๋ ๋ค ๋ธ๋ผ์ฐ์ ์ ์ ์ฅ์ ํ๋ ๋ฐ์ดํฐ
* - local Storage : ๋์ผํ PC + ๋์ผํ ๋ธ๋ผ์ฐ์ ์ผ ๊ฒฝ์ฐ, ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ ์ฌ ์ ์์
* ex. ์๋ ๋ก๊ทธ์ธ, ์ฅ๋ฐ๊ตฌ๋, ...
* => ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ซ๋๋ผ๋ ๋ฐ์ดํฐ๊ฐ ๊ณ์ํด์ ์ ์ฅ๋์ด์๋ค.
* - session Storage : ๋ธ๋ผ์ฐ์ ์ฐฝ์ด ์ข
๋ฃ๋๋ฉด ์ธ์
๋ ํจ๊ป ์ข
๋ฃ๋๋ฉด์ ๋ฐ์ดํฐ ์๋ฉธ๋จ
* ex. ๋ก๊ทธ์ธ ๊ธฐ๋ก
*/
๋ฐฑ์๋, ํ๋ก ํธ ์ด๋์ ๊ด๋ฆฌํ ๊ฒ์ธ์ง๋ ์์
์์์ ์ฐจ์ด์ผ๋ฟ!
1. Local Storage
์๋ ๋ก๊ทธ์ธ, ์ฅ๋ฐ๊ตฌ๋์ ๊ฐ์ด ๋ณด์์ด ๋ฑํ ํ์ํ์ง ์์ ๋ ์ฌ์ฉ
1.1. ์ ์ฅํ๊ธฐ
localStorage.setItem('์ด๋ฆ', ์ ์ฅํ ๊ฐ)
const setLocal = ()=>{
console.log('local storage ์ ์ฅ', localText);
//๋ก์ปฌ ์คํ ๋ฆฌ์ง์ ์ ์ฅํ๊ธฐ
localStorage.setItem('data', localText)
}
๋ก์ปฌ ์คํ ๋ฆฌ์ง๋ ๋ฐ์ดํฐ๊ฐ ๋์ ๋์ง ์์
๋์ ํ๊ณ ์ถ์ผ๋ฉด ์๋ก์ด ์ด๋ฆ์ ์ง์ ํ์ฌ ์ ์ฅํด์ผ ํจ!
1.2. ๊ฐ์ ธ์ค๊ธฐ
localStorage.getItem('๊ฐ์ ธ์ค๊ณ ์ถ์๋ฐ์ดํฐ์ด๋ฆ')
const getLocal = ()=>{
//๋ก์ปฌ ์คํ ๋ฆฌ์ง์์ ๋ฐ์ดํฐ ๊บผ๋ด์ค๊ธฐ
setLocalText2(localStorage.getItem('data'))
}
1.3. ์ญ์ ํ๊ธฐ
localStorage.removeItem('์ญ์ ํ ๋ฐ์ดํฐ ์ด๋ฆ')
{/* .clear() => ์คํ ๋ฆฌ์ง ์ด๊ธฐํ
localStorage.clear()
*/}
<button onClick={()=>{window.localStorage.removeItem('data')}}>local storage ์ญ์ </button>
2. Session Storage
2.1. ์ ์ฅํ๊ธฐ
sessionStorage.setItem('์ด๋ฆ', ์ ์ฅํ ๊ฐ)
const setSession = ()=>{
console.log('session storage ์ ์ฅ', sessionText);
sessionStorage.setItem('data', sessionText)
}
2.2. ๊ฐ์ ธ์ค๊ธฐ
sessionStorage.getItem('๊ฐ์ง๊ณ ์ฌ ๋ฐ์ดํฐ ์ด๋ฆ')
const getSession = ()=>{
setSessionText2(sessionStorage.getItem('data'))
}
2.3. ์ญ์ ํ๊ธฐ
๋ณดํต ๋ก๊ทธ์์ ๊ธฐ๋ฅ์ ๋ง์ด ์ฌ์ฉ
sessionStorage.removeItem('์ญ์ ํ ๋ฐ์ดํฐ ์ด๋ฆ')
<button onClick={()=>{window.sessionStorage.removeItem('data')}}>session storage ์ญ์ </button>
๐ ๊ตฌํ ํ๋ฉด
๐ ์ค๋ฅ
too many Render ~~~~ (๋ฌดํ๋ฃจํ)
๊ดํธ ์๋ชป ์ ๋ ฅํ์ ์ ์์
๐ ์ ์ฒด ์ฝ๋
import React from 'react'
import { useState } from 'react'
const Storage = () => {
/**
* Local Storage vs Session Storage
* - ์ฃผ์์ฌํญ! ๋ ๋ค ๋ธ๋ผ์ฐ์ ์ ์ ์ฅ์ ํ๋ ๋ฐ์ดํฐ
* - local Storage : ๋์ผํ PC + ๋์ผํ ๋ธ๋ผ์ฐ์ ์ผ ๊ฒฝ์ฐ, ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ ์ฌ ์ ์์
* ex. ์๋ ๋ก๊ทธ์ธ, ์ฅ๋ฐ๊ตฌ๋, ...
* => ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ซ๋๋ผ๋ ๋ฐ์ดํฐ๊ฐ ๊ณ์ํด์ ์ ์ฅ๋์ด์๋ค.
* - session Storage : ๋ธ๋ผ์ฐ์ ์ฐฝ์ด ์ข
๋ฃ๋๋ฉด ์ธ์
๋ ํจ๊ป ์ข
๋ฃ๋๋ฉด์ ๋ฐ์ดํฐ ์๋ฉธ๋จ
* ex. ๋ก๊ทธ์ธ ๊ธฐ๋ก
*/
const [localText, setLocalText] = useState('')
const [localText2, setLocalText2] = useState('')
const setLocal = ()=>{
console.log('local storage ์ ์ฅ', localText);
//๋ก์ปฌ ์คํ ๋ฆฌ์ง์ ์ ์ฅํ๊ธฐ
localStorage.setItem('data', localText)
}
const getLocal = ()=>{
//๋ก์ปฌ ์คํ ๋ฆฌ์ง์์ ๋ฐ์ดํฐ ๊บผ๋ด์ค๊ธฐ
setLocalText2(localStorage.getItem('data'))
}
const [sessionText, setSessionText] = useState('')
const [sessionText2, setSessionText2] = useState('')
const setSession = ()=>{
console.log('session storage ์ ์ฅ', sessionText);
sessionStorage.setItem('data', sessionText)
}
const getSession = ()=>{
setSessionText2(sessionStorage.getItem('data'))
}
return (
<div className='list-container'>
<div>
<h2>Local Storage : </h2>
<input type="text" onChange={(e)=>{setLocalText(e.target.value)}}></input>
<button onClick={setLocal}>์ ์ฅ</button>
<button onClick={getLocal}>๊ฐ์ ธ์ค๊ธฐ</button><br></br>
{localText2}
</div>
<div>
<h2>Session Storage : </h2>
<input type="text" onChange={(e)=>{setSessionText(e.target.value)}}></input>
<button onClick={setSession}>์ ์ฅ</button>
<button onClick={getSession}>๊ฐ์ ธ์ค๊ธฐ</button><br/>
{sessionText2}
</div>
<div>
{/* .clear() => ์คํ ๋ฆฌ์ง ์ด๊ธฐํ
localStorage.clear()
*/}
<button onClick={()=>{window.localStorage.removeItem('data')}}>local storage ์ญ์ </button>
<button onClick={()=>{window.sessionStorage.removeItem('data')}}>session storage ์ญ์ </button>
</div>
</div>
)
}
export default Storage
๋ฐ์ํ
'Study > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] ๊ฐ๋ฐ ์ฐธ๊ณ ์ฌ์ดํธ ๋ฐ ํ ํ๋ฆฟ ์ ์ฉํ๋ ๋ฒ (2023.06.09) (0) | 2023.06.09 |
---|---|
[React] ์นด์นด์ค์ง๋API ์ฌ์ฉํ๊ธฐ (2023.06.05) (0) | 2023.06.05 |
[React] useEffect๋ฅผ ํตํด openAPI ๊ฐ์ ธ์ค๊ธฐ (Fetch, Axios)(2023.05.26) (0) | 2023.05.29 |
[React] ์ฐธ์ฐธ์ฐธ ๊ฒ์ ๋ง๋ค๊ธฐ (2023.05.26) (0) | 2023.05.29 |
[React] Lifecycle (2023.05.24) (0) | 2023.05.24 |