@one-view/global-context
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

@one-view/global-context

Installation

With NPM

npm install @one-view/global-context

With Yarn

yarn add @one-view/global-context

Usage

Create Context

Create context using useCreateContext hook. This hook will automatically destroy the context when the component unmounted.

import { useCreateContext } from '@one-view/global-context';

const MyComponent = () => {
    useCreateContext('my-namespace');
    ...
}

or create the context manually using createContext helper.

import { createContext } from '@one-view/global-context';

createContext('default', { foo: 'bar' });

Access & Update Context Value

Access and update context inside react component with useGlobalContext hook...

import { useGlobalContext } from '@one-view/global-context';

const MyComponent = () => {
    const { value: foo, update: setFoo } = useGlobalContext<string>('default', 'foo');

    return <button onClick={() => setFoo('baz')}>{foo}</button>;
};

...or anywhere with getContextValue and setContextValue.

import { getContextValue, setContextValue } from '@one-view/global-context';

const foo = getContextValue<string>('default', 'foo');
setContextValue('default', 'foo', 'bar');

Destroy Context

Destroy context with destroyContext helper.

import { destroyContext } from '@one-view/global-context;

destroyContext('default')

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.2.01latest

Version History

VersionDownloads (Last 7 Days)Published
1.2.01
1.1.00
1.0.00
0.0.1-alpha-30

Package Sidebar

Install

npm i @one-view/global-context

Weekly Downloads

1

Version

1.2.0

License

ISC

Unpacked Size

9.89 kB

Total Files

12

Last publish

Collaborators

  • alipsidik
  • ramadimasatria
  • forex
  • ichsanrp
  • alicanakyuz
  • gabrielang-fp
  • hendra.wijaya
  • fp_apac_jonathan.sudibya
  • jianann-fp
  • shing-fp