Home / File/ createContext.ts — react Source File

createContext.ts — react Source File

Architecture documentation for createContext.ts, a typescript file in the react codebase. 1 imports, 1 dependents.

File typescript PlaygroundApp Stores 1 imports 1 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  49642e97_a10e_5f44_62bd_268b445bc02f["createContext.ts"]
  ac587885_e294_a1e9_b13f_5e7b920fdb42["react"]
  49642e97_a10e_5f44_62bd_268b445bc02f --> ac587885_e294_a1e9_b13f_5e7b920fdb42
  73168655_c854_d3d1_50a0_b37f865f3c7e["StoreContext.tsx"]
  73168655_c854_d3d1_50a0_b37f865f3c7e --> 49642e97_a10e_5f44_62bd_268b445bc02f
  style 49642e97_a10e_5f44_62bd_268b445bc02f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import React from 'react';

/**
 * Replacement to React.createContext.
 *
 * Does not take any default value and avoids non-null assertions when using
 * the value of the context, like the following scenario.
 *
 * ```ts
 * const StoreDispatchContext = useContext<Dispatch<ReducerAction>>(null);
 * const dispatchStore = useContext(StoreDispatchContext);
 * ...
 * dipatchStore!({ ... });
 * ```
 *
 * Instead, it throws an error when `useContext` is not called within a
 * Provider with a value.
 */
export default function createContext<T>(): {
  useContext: () => NonNullable<T>;
  Provider: React.Provider<T | null>;
} {
  const context = React.createContext<T | null>(null);

  function useContext(): NonNullable<T> {
    const c = React.useContext(context);
    if (!c)
      throw new Error('useContext must be within a Provider with a value');
    return c;
  }

  return {useContext, Provider: context.Provider};
}

Domain

Subdomains

Functions

Dependencies

  • react

Frequently Asked Questions

What does createContext.ts do?
createContext.ts is a source file in the react codebase, written in typescript. It belongs to the PlaygroundApp domain, Stores subdomain.
What functions are defined in createContext.ts?
createContext.ts defines 1 function(s): createContext.
What does createContext.ts depend on?
createContext.ts imports 1 module(s): react.
What files import createContext.ts?
createContext.ts is imported by 1 file(s): StoreContext.tsx.
Where is createContext.ts in the architecture?
createContext.ts is located at compiler/apps/playground/lib/createContext.ts (domain: PlaygroundApp, subdomain: Stores, directory: compiler/apps/playground/lib).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free