feat(snippets): introduce reusable code chunks section, add cms support, and fix navbar turkish characters
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
---
|
||||
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;
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user