๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript/Nomad Coders

[TIL] ๐Ÿ’ก Day 7 - Javascript value getting, saving, loading ํ•˜๋Š” ๋ฐฉ๋ฒ•

by vividmin 2022. 4. 1.
320x100

โœ” ์ด ํฌ์ŠคํŒ…์€ ๋…ธ๋งˆ๋“œ์ฝ”๋” - "๋ฐ”๋‹๋ผ JS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ๋ฅผ ๋“ค์œผ๋ฉด์„œ ๊นƒํ—ˆ๋ธŒ์— ๊ธฐ๋กํ•œ ํ•„๊ธฐ๋ฅผ ์˜ฎ๊ธด ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

                                          ๐Ÿ“Œ value getting, saving, loading ํ•˜๋Š” ๋ฐฉ๋ฒ•

 

๐ŸŒฑ Getting Username

๐Ÿ‘‰ ์‚ฌ์šฉ์ž๊ฐ€ Input์— ์ž…๋ ฅํ•œ ๊ฐ’์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ธฐ

  • Form ์ œ์ถœ ํ›„ Form์„ ํ™”๋ฉด์—์„œ ์‚ฌ๋ผ์ง€๊ฒŒํ•˜๊ธฐ(CSS ์ด์šฉ)
    • CSS class hidden ์ด์šฉํ•˜๊ธฐ [ display : none ]
  • input์˜ value๋ฅผ username์œผ๋กœ ํ• ๋‹น
    • const username = loginInput.value;
  • ์˜ˆ์‹œ์ฝ”๋“œ
<!-- HTML ํŒŒ์ผ --> 

<form class="hidden" id="login-form"> 
<input required maxlength="15" type="text" placeholder="What is your name?"> 
<input type="submit" value="Log in"> 
</form> 
<h1 id="greeting" class="hidden"></h1>`

 

    // js ํŒŒ์ผ

    const loginForm = document.querySelector("#login-form");
    const loginInput = document.querySelector("#login-form input");
    const loginBtn = document.querySelector("#login-form button");
    const greeting = document.querySelector("#greeting");

    //์ผ๋ฐ˜์ ์œผ๋กœ string๋งŒ ํฌํ•จ๋œ ๋ณ€์ˆ˜๋Š” ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•˜๊ณ , 
	//  string์„ ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
    const HIDDEN_CLASSNAME = "hidden";

    function onLoginSubmit(event) {
        event.preventDefault();
        loginForm.classList.add(HIDDEN_CLASSNAME)

        //์œ ์ €๋„ค์ž„์„ ๋ณ€์ˆ˜๋กœ ์ €์žฅ
        const username = loginInput.value;  

        // "Hello " + username ๋ณด๋‹ค ํŽธ๋ฆฌํ•œ ํ‘œ๊ธฐ๋ฒ• ( `${๊ฐ’}` ์ •๊ทœํ‘œํ˜„์‹)
        greeting.innerText = `Hello ${username}`;    
        greeting.classList.remove(HIDDEN_CLASSNAME)
    }

    loginForm.addEventListener("submit", onLoginSubmit);

 

 

 

๐ŸŒฑ Saving Username

๐Ÿ‘‰ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ Input์˜ value๋ฅผ ์ €์žฅํ•ด๋‘๋Š” ๋ฐฉ๋ฒ•

  • LocalStorage ์‚ฌ์šฉํ•˜๊ธฐ
    • ๋ธŒ๋ผ์šฐ์ €์— ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
    • ๋ฏธ๋‹ˆ DB์˜ ๋Š๋‚Œ
    • localStorage.setItem : ๊ฐ’์„ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉ
    • localStorage.getItem : ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉ
    • localStorage.removeItem : ๊ฐ’์„ ์ง€์šธ ๋•Œ ์‚ฌ์šฉ
  • ์˜ˆ์‹œ์ฝ”๋“œ
function onLoginSubmit(event) { 
    event.preventDefault(); 
    loginForm.classList.add(HIDDEN_CLASSNAME); 
    const username = loginInput.value; // localStorage ์‚ฌ์šฉ 
    localStorage.setItem("username", username); 
    greeting.innerText = `Hello ${username}`; 
    greeting.classList.remove(HIDDEN_CLASSNAME) 
}

 

  • ๊ฐœ์„ ํ•  ๋ฐฉํ–ฅ
    • ํ˜„์žฌ → ์ด๋ฆ„ ์ž…๋ ฅ -> ๋กœ๊ทธ์ธ๋ฒ„ํŠผ or ์—”ํ„ฐ -> Hello ์ด๋ฆ„! -> ์ด๋ฆ„์ด LocalStorage์— ์ €์žฅ
      But! ์ƒˆ๋กœ๊ณ ์นจ ์‹œ username๊ฐ’์€ localStrage์— ์žˆ์ง€๋งŒ ์—ฌ์ „ํžˆ form์€ ๋‹ค์‹œ ๋‚˜ํƒ€๋‚œ๋‹คโ—
    • ๐Ÿ‘‰ localStorage์—
      • username๊ฐ’๐Ÿ™†๐Ÿป‍โ™€๏ธ → Hello ์ด๋ฆ„ ํ™”๋ฉด ๋ณด์—ฌ์คŒ
      • username ๐Ÿ™…๐Ÿป‍โ™€๏ธ → ๋กœ๊ทธ์ธ ํผ ๋‚˜์˜ค๋„๋ก ์„ค์ •

 

 

 

๐ŸŒฑ Loading Username

๐Ÿ‘‰ localStorage์— ๊ฐ’์˜ ์œ ๋ฎค์— ๋”ฐ๋ผ์„œ ํ™”๋ฉด์„ ๋‹ค๋ฅด๊ฒŒ ๋ณด์ด๋„๋ก ์„ค์ •ํ•˜๊ธฐ

  • ํ•ด์•ผํ•  ์ผ
    • username์ด ๋„ˆ๋ฌด ๋งŽ์ด ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ์Œ → ์˜คํƒ€ ์‹œ ์˜ค๋ฅ˜๊ฐ€๋Šฅ์„ฑ → USERNAME_KEY๋กœ ๋ณ€์ˆ˜์„ค์ •
  • ์˜ˆ์‹œ์ฝ”๋“œ
const USERNAME_KEY = "username"; 

function onLoginSubmit(event) { 
    event.preventDefault(); 
    loginForm.classList.add(HIDDEN_CLASSNAME); 
    const USERNAME_KEY = loginInput.value; 
    localStorage.setItem("USERNAME_KEY", username);
    greeting.innerText = `Hello ${username}`; 
    greeting.classList.remove(HIDDEN_CLASSNAME); 
} 

const savedUsername = localStorage.getItem("USERNAME_KEY");

// ์ด ๊ตฌ๋ฌธ์„ ์™„์„ฑํ•˜๊ธฐ ์œ„ํ•ด form, greeting๋ชจ๋‘ hidden์ƒํƒœ๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
if (savedUsername === null) {
    // show the form
    loginForm.classList.remove(HIDDEN_CLASSNAME);
    loginForm.addEventListener("submit", onLoginSubmit);
}else {
    // show the greetings
    greeting.innerText = Hello ${savedUsername};
    greeting.classList.remove(HIDDEN_CLASSNAME);
}
  • ๊ฐœ์„  ํ•„์š”ํ•œ ์   
        : ๊ฐ™์€ ๋™์ž‘์„ 2๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ณ  ์žˆ์Œ  
        onLoginSubmit( )๊ณผ if๋ฌธ์—์„œ  
           ๐Ÿ‘‰ `(greeting.innerText, greeting.classList.remove(HIDDEN_CLASSNAME))` ๋ฐ˜๋ณต ๋œ๋‹ค.
  • ์ด๋Ÿฐ๊ฒƒ์€ ํ•จ์ˆ˜๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋” ์ข‹๋‹ค.  
        → `paintGreetings ๋ผ๋Š” ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ
  • ๊ฐœ์„ ๋œ ์ฝ”๋“œ
const loginForm = document.querySelector("#login-form"); 
const loginInput = document.querySelector("#login-form input"); 
const loginBtn = document.querySelector("#login-form button"); 
const greeting = document.querySelector("#greeting");

const HIDDEN_CLASSNAME = "hidden"; 
const USERNAME_KEY = "username"; 

function onLoginSubmit(event) { 
  event.preventDefault(); 
  loginForm.classList.add(HIDDEN_CLASSNAME); 
  const USERNAME_KEY = loginInput.value; 
  localStorage.setItem("USERNAME_KEY", username); 
  paintGreetings(username); 
} 

function paintGreetings(username) { 
  greeting.innerText = `Hello ${username}`; 
  greeting.classList.remove(HIDDEN_CLASSNAME); 
} 

const savedUsername = localStorage.getItem("USERNAME_KEY"); 

if (savedUsername === null) { 
  // show the form 
  loginForm.classList.remove(HIDDEN_CLASSNAME); 
  loginForm.addEventListener("submit", onLoginSubmit); 
}else { 
  // show the greetings 
  paintGreetings(savedUsername); 
}
  • paintGreetingํ•จ์ˆ˜`๋ฅผ ์ด์šฉํ•ด์„œ ์ฝ”๋“œ ๋ฐ˜๋ณต์„ ์ค„์˜€์ง€๋งŒ,
    ์šฐ๋ฆฌ๊ฐ€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์œ„์น˜์— ๋”ฐ๋ผ ์œ ์ €์ •๋ณด๋Š” ๋‹ค๋ฅธ ๊ณณ์—์„œ ์˜ค๊ฒŒ ๋œ๋‹ค.
    • local Storage์— ์œ ์ €์ •๋ณด โญ• → ๊ฑฐ๊ธฐ์„œ ์œ ์ €์ •๋ณด๋ฅผ ๋ฐ›์•„์„œ ์ธ์ž๋กœ ๋„ฃ๋Š”๋‹ค.
    • ๋งŒ์•ฝ local storage์— ์œ ์ €์ •๋ณด โŒ → form์˜ submit์„ ๊ธฐ๋‹ค๋ฆผ → form์ด submit๋˜๋ฉด input์œผ๋กœ๋ถ€ํ„ฐ ์œ ์ €์ •๋ณด๋ฅผ ๋ฐ›์Œ → input์—์„œ ๋ฐ›์€ user๋ฅผ ๊ฐ€์ง„ patint Greetings๋ฅผ ํ˜ธ์ถœ

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€