Skip to content

Commit

Permalink
docs: useSafeContext
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunjinee committed May 9, 2024
1 parent c276acc commit 11830e2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/useSafeContext/useSafeContext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# useSafeContext

useSafeContext 훅은 사용하고 싶은 Context를 안전하게 사용할 수 있도록 도와주는 훅입니다. 이 훅은 Context를 사용할 때, Context가 제공하는 Provider가 존재하지 않을 때, 에러를 발생시키고 적절한 에러메시지를 제공합니다.

## type

```ts
const useSafeContext: <T>(
unsafeContext: Context<T>,
customMessage?: CustomMessage,
) => NonNullable<T>;
```

## example

```tsx
const AuthContext = React.createContext<User | null>(null);

const useAuth = () =>
useSafeContext(
AuthContext,
'AuthContext는 AuthProvider 내에서만 사용되어야 합니다',
);
```

0 comments on commit 11830e2

Please sign in to comment.