typesafe-context-hook
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

typesafe-context-hook

A react-typescript utility for creating typesafe React context hooks.

Installation

pnpm i typesafe-context-hook
npm i typesafe-context-hook
yarn i typesafe-context-hook

Usage

import typesafeContextHook from 'typesafe-context-hook';
import { useState } from 'react';

export const { useName, NameProvider, withName } = typesafeContextHook('Name', () => {
  const [name, setName] = useState('John Doe');
  return { name, setName };
});
// In another file
import { useName, NameProvider } from './name-context.tsx';

function App() {
  return (
    <NameProvider>
      <AnotherComponent />
    </NameProvider>
  );
}

function AnotherComponent() {
  const { name, setName } = useName();
  // Now you can use name and setName in this component
}

Package Sidebar

Install

npm i typesafe-context-hook

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

17.5 kB

Total Files

6

Last publish

Collaborators

  • mrthinger