Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/src/utils/create-safe-context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { type Provider, createContext, useContext } from 'react';

type NullSymbolType = typeof NullSymbol;
const NullSymbol = Symbol('Null');

export type CreateContextReturn<T> = [Provider<T>, () => T];

export function createSafeContext<T>(displayName?: string): CreateContextReturn<T> {
const Context = createContext<T | NullSymbolType>(NullSymbol);
const NullSymbol = Symbol('Null');

const Context = createContext<T | typeof NullSymbol>(NullSymbol);
Context.displayName = displayName ?? 'SafeContext';

function useSafeContext() {
Expand Down