# named-context-react

> ### Installation

Latest version **1.0.1** (published 2024-06-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install named-context-react
pnpm add named-context-react
yarn add named-context-react
bun add named-context-react
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2024-06-30 |
| First published | 2023-05-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Maxim Molochkov |
| Keywords | react, context |

## Links

- npm: https://www.npmjs.com/package/named-context-react
- Repository: https://github.com/klaseca/named-context
- Issues: https://github.com/klaseca/named-context/issues
- npm.io page: https://npm.io/package/named-context-react

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2024-06-30
- 1.0.0 — 2023-05-01

## README

## React

### Installation

```sh
npm i named-context-react
```

### API

```typescript
const values = createNamedContext(params);
```

#### params

| Property           | Default value | Description                                                       |
| ------------------ | ------------- | ----------------------------------------------------------------- |
| `name`             | -             | Name of your context. Use as prefix or postfix in returned values |
| `defaultValue`     | -             | Default value of your context                                     |
| `isNullishAllowed` | `false`       | Property that allows the use of `nullish` values in context       |

#### values

| Property                 | Description                                                              |
| ------------------------ | ------------------------------------------------------------------------ |
| `${params.name}Context`  | Object that includes `Provider` and `Consumer` components                |
| `${params.name}Provider` | `Context.Provider` component. Alias for `${params.name}Context.Provider` |
| `${params.name}Consumer` | `Context.Consumer` component. Alias for `${params.name}Context.Consumer` |
| `use${params.name}`      | Hook to work with data from `${params.name}Context`                      |

### Usage

#### Create context with defined `defaultValue`

```typescript
import { createNamedContext } from 'named-context-react';

const { DataContext, DataProvider, DataConsumer, useData } = createNamedContext(
  { name: 'Data', defaultValue: 0 }
);
```

#### Create context without defined `defaultValue`

If you do not want to pass `defaultValue` when creating a context, use helper function `valueType` to specify type of value you expect

```tsx
import { createNamedContext, valueType } from 'named-context-react';

const { DataContext, DataProvider, DataConsumer, useData } = createNamedContext(
  { name: 'Data', defaultValue: valueType<number>() }
);

// ...

<DataContext.Provider value={1} />
// or
<DataProvider value={1} />
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  `value` prop expects a value of type `number` (as specified in `valueType` function)
*/

// ...

const data = useData();
/* ^^^^^^^^^^^^^^^^^^^^
  `data` is of type `number` (as specified in `valueType` function)
  If `useData` is used outside `DataContext.Provider` or `DataProvider` or if `value` prop is not passed, an error will be throw
*/
```

If `undefined` or `null` value should be allowed, set `isNullishAllowed` param to `true`

```tsx
import { createNamedContext, valueType } from 'named-context-react';

const { NullishContext, NullishProvider, NullishConsumer, useNullish } = createNamedContext(
  { name: 'Nullish', defaultValue: valueType<string>(), isNullishAllowed: true }
);

// ...

<NullishContext.Provider value={undefined} />
// or
<NullishProvider value={''} />
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  `value` prop expects a value of type `string` (as specified in `valueType` function) or `undefined` (because `isNullishAllowed` is `true`)
*/

// ...

const data = useNullish();
/* ^^^^^^^^^^^^^^^^^^^^^^^
  `data` is of type `string` (as specified in `valueType` function) or `undefined` (because `isNullishAllowed` is `true`)
*/
```

---
_Source: https://npm.io/package/named-context-react · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
