1.0.1 • Published 1 year ago

@vtaits/react-required-context v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

NPM

@vtaits/react-required-context

React context that has no default value and must be defined to be used.

You shouldn't make fallback for complex structure, or write extra code to check if context is defined or not.

import {
  createContext,
  useContext,
} from '@vtaits/react-required-context';

Examples

With defined provider of context

import {
  createContext,
  useContext,
} from '@vtaits/react-required-context';

const Context = createContext<VeryComplesStructure>(); // You shouldn't define fallback or something like `null` value

function ChildComponent() {
  const value = useContext(Context); // `value` is equal to `complexValue` that provided below

  return <div />;
}

function App() {
  return (
    <Context.Provider value={complexValue}> // complexValue muse be of type `VeryComplesStructure`
      <ChildComponent />
    </Context.Provider>
  );
}

Without defined provider of context

import {
  createContext,
  useContext,
} from '@vtaits/react-required-context';

const Context = createContext<VeryComplesStructure>(); // You shouldn't define fallback or something like `null` value

function App() {
  const value = useContext(Context); // throws an error because there is no provider for this context

  return (
    <div />
  );
}