25 lines
639 B
Markdown
25 lines
639 B
Markdown
---
|
|
title: "useLocalStorage Hook"
|
|
language: "typescript"
|
|
category: "React Hooks"
|
|
description: "localStorage ile senkronize çalışan bir React hook'u."
|
|
---
|
|
|
|
```typescript
|
|
import { useState, useEffect } from "react";
|
|
|
|
export function useLocalStorage<T>(key: string, initialValue: T) {
|
|
const [value, setValue] = useState<T>(() => {
|
|
if (typeof window === "undefined") return initialValue;
|
|
const stored = localStorage.getItem(key);
|
|
return stored ? JSON.parse(stored) : initialValue;
|
|
});
|
|
|
|
useEffect(() => {
|
|
localStorage.setItem(key, JSON.stringify(value));
|
|
}, [key, value]);
|
|
|
|
return [value, setValue] as const;
|
|
}
|
|
```
|