1.2.0 • Published 5 years ago
@one-view/global-context v1.2.0
@one-view/global-context
Installation
With NPM
npm install @one-view/global-contextWith Yarn
yarn add @one-view/global-contextUsage
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')1.2.0
5 years ago
1.1.0
5 years ago
1.0.0
5 years ago
0.0.1-alpha-3
5 years ago
0.0.1-alpha-2
5 years ago
0.0.1-alpha
5 years ago