@phragon-react/use-create-context
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

@phragon-react/use-create-context

Helper hooks for react createContext function and useContext hook

❯ Install

$ npm install --save @phragon-react/use-create-context

Usage

import { createContext, createStrictContext } from "@phragon-react/use-create-context";

const [Provider, useContext] = createContext();
const [StrictProvider, useStrictContext] = createStrictContext();

function Foo() {
	return (
		<Provider value={"test"}>
			<Bar />
		</Provider>
	);
}

function FooStrict() {
	return (
		<StrictProvider value={"test"}>
			{/* Successfull render, <Bar /> is null */}
			<Bar />
			<BarStrict />
		</StrictProvider>
	);
}

function FooError() {
	return (
		<>
			{/* Successfull render, <Bar /> is null */}
			<Bar />
			{/* Render failed */}
			<BarStrict />
		</>
	);
}

function Bar() {
	const context = useContext();
	return (
		<div>{context}</div>
	);
}

function BarStrict() {
	const context = useStrictContext();
	return (
		<div>{context}</div>
	);
}

Readme

Keywords

none

Package Sidebar

Install

npm i @phragon-react/use-create-context

Weekly Downloads

0

Version

0.0.1

License

MIT

Unpacked Size

6.31 kB

Total Files

7

Last publish

Collaborators

  • websoftlab