Skip to content

[URH-21] useLocalStorage 신규 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 22, 2024
Merged

Conversation

jeongbaebang
Copy link
Member

@jeongbaebang jeongbaebang commented Jul 12, 2024

👾 Pull Request

  • 작업명: useLocalStorage
  • 상태: 신규

1️⃣ Spec

  • 로컬 스토리지를 쉽게 사용하도록 하는 hook
  • key값을 받아서 해당 key에 저장되어 있는 value와 data를 해당 key에 저장하는 setter 함수를 리턴
  • 서로 다른 타입의 값 처리시 경고 문구 활성

2️⃣ 변경 사항

  • 변경 사항 없음

3️⃣ 예시 코드

import React from 'react';
import useLocalStorage from './hooks/useLocalStorage';

function App() {
  // 'count' 키와 초기 값 0을 사용하여 로컬 스토리지에 저장된 값을 관리
  const [count, setCount] = useLocalStorage<number>('count', 0);

  const increment = () => {
    setCount((prevCount) => prevCount + 1);
  };

  // 잘못된 타입의 값을 설정하려는 함수 (타입 에러 발생 예시)
  const wrong = () => {
    /**
     * Failed to set item in localStorage MatchError:
     * New value type does not match stored value type
     * current:20 + 1->string, prev:20->number
     */
    setCount((prevCount) => `${prevCount} + 1`); // 숫자 타입에 문자열을 할당하려고 시도함
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
      <button onClick={wrong}>wrong</button>
    </div>
  );
}

export default App;

4️⃣ 관련 문서 (선택 사항)

@jeongbaebang jeongbaebang self-assigned this Jul 12, 2024
Copy link
Collaborator

@foresec foresec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수고하셨습니다! 좀 더 구조화된 훅으로 작성하셨네요:)

Copy link
Member

@bicochan bicochan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

멋져요 정배 님!👍🏻✨

Copy link
Member

@Choozii Choozii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✈️

@jeongbaebang jeongbaebang changed the title ✨ feat: useLocalStorage 훅 추가 [URH-21] useLocalStorage 신규 Jul 21, 2024
@jeongbaebang jeongbaebang merged commit 04ae904 into master Jul 22, 2024
@bicochan bicochan deleted the feature/use-local-storage branch July 25, 2024 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants