Skip to content

Commit 4f4b10d

Browse files
authored
merge: useLocalStorage 스토리북 작성 (#19)
merge: useLocalStorage 스토리북 작성 (#19)
2 parents 81850ef + 1c56559 commit 4f4b10d

File tree

5 files changed

+122
-1
lines changed

5 files changed

+122
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rapiders/react-hooks",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"description": "react hooks for fast development",
55
"main": "dist/esm/index.js",
66
"types": "dist/esm/index.d.ts",

src/stories/useLocalStorage/Docs.mdx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Canvas, Meta, Description } from '@storybook/blocks';
2+
import * as LocalStorage from './LocalStorage.stories';
3+
4+
<Meta of={LocalStorage} />
5+
6+
# useLocalStorage
7+
8+
LocalStorage 내부 데이터를 선언적으로, type safe하게 관리할 수 있게 하는 훅입니다.
9+
10+
## 제네릭
11+
12+
제네릭을 사용하여 LocalStorage에 저장되는 데이터 타입을 지정하여 안전하게 데이터를 넣고 꺼낼 수 있습니다.
13+
14+
## 함수 인자
15+
16+
`key`: LocalStorage의 Key입니다.
17+
18+
`initialValue`: LocalStorage Key의 최초 Value입니다.
19+
20+
`options (UseLocalStorageOptions | undefined)`: LocalStorage에 저장되고, 값을 가져올때 적용할 직렬화 함수와 역직렬화함수를 options으로 지정할 수 있습니다.
21+
22+
## 반환값
23+
24+
`value`: 현재 LocalStorage에 들어있는 값입니다.
25+
26+
`saveValue`: LocalStorage의 값을 변경하는 함수입니다.
27+
28+
`removeValue`: LocalStorage에 저장된 값을 제거하는 함수입니다.
29+
30+
```tsx
31+
function LocalStorage() {
32+
const [storageValue, setStorageValue] = useLocalStorage('key', 'value');
33+
const { value, onChange } = useInput('');
34+
35+
return (
36+
<div className={flexCol}>
37+
<div className={info}>Local Storage (key) 에 저장된 값 : {storageValue}</div>
38+
<div>새로고침을 통해 확인해보세요!</div>
39+
40+
<div className={flex}>
41+
<input type="text" onChange={onChange} className={input} placeholder="값 변경하기!" />
42+
<button onClick={() => setStorageValue(value)} className={button}>
43+
적용
44+
</button>
45+
</div>
46+
</div>
47+
);
48+
}
49+
```
50+
51+
<Canvas of={LocalStorage.defaultStory} />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { style } from '@vanilla-extract/css';
2+
3+
export const flex = style({
4+
display: 'flex',
5+
gap: 10,
6+
});
7+
8+
export const flexCol = style({
9+
display: 'flex',
10+
flexDirection: 'column',
11+
gap: 10,
12+
});
13+
14+
export const info = style({
15+
fontSize: 20,
16+
});
17+
export const input = style({
18+
padding: 15,
19+
fontSize: 15,
20+
borderRadius: 15,
21+
});
22+
23+
export const button = style({
24+
fontSize: 15,
25+
padding: 15,
26+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Meta, StoryObj } from '@storybook/react';
2+
import LocalStorage from './LocalStorage';
3+
4+
const meta = {
5+
title: 'hooks/useLocalStorage',
6+
component: LocalStorage,
7+
parameters: {
8+
layout: 'centered',
9+
docs: {
10+
canvas: {},
11+
},
12+
},
13+
} satisfies Meta<typeof LocalStorage>;
14+
15+
export default meta;
16+
17+
type Story = StoryObj<typeof meta>;
18+
19+
export const defaultStory: Story = {
20+
args: {},
21+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import useInput from '@/useInput/useInput';
2+
import useLocalStorage from '@/useLocalStorage/useLocalStorage';
3+
import React from 'react';
4+
import { button, flex, flexCol, info, input } from './LocalStorage.css';
5+
6+
export default function LocalStorage() {
7+
const [storageValue, setStorageValue] = useLocalStorage('key', 'value');
8+
const { value, onChange } = useInput('');
9+
10+
return (
11+
<div className={flexCol}>
12+
<div className={info}>Local Storage (key) 에 저장된 값 : {storageValue}</div>
13+
<div>새로고침을 통해 확인해보세요!</div>
14+
15+
<div className={flex}>
16+
<input type="text" onChange={onChange} className={input} placeholder="값 변경하기!" />
17+
<button onClick={() => setStorageValue(value)} className={button}>
18+
적용
19+
</button>
20+
</div>
21+
</div>
22+
);
23+
}

0 commit comments

Comments
 (0)