# context-simplifier

> Utility to simplify context api usage in react

Latest version **0.0.4** (published 2021-06-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install context-simplifier
pnpm add context-simplifier
yarn add context-simplifier
bun add context-simplifier
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2021-06-27 |
| First published | 2021-06-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 20.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Harshdand |
| Maintainers | harshdand |
| Keywords | context simplifier, react, react context, context |

## Links

- npm: https://www.npmjs.com/package/context-simplifier
- Repository: https://github.com/Harshdand/context-simplifier
- npm.io page: https://npm.io/package/context-simplifier

## 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

- 0.0.4 (latest) — 2021-06-27
- 0.0.3 — 2021-06-27
- 0.0.2 — 2021-06-13

## README

# context-simplifier
### Utility to simplify context api usage in react

[![NPM](https://img.shields.io/npm/v/context-simplifier.svg)](https://www.npmjs.com/package/context-simplifier) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Install

```bash
npm install --save context-simplifier
```

```bash
yarn add context-simplifier
```
<br/>

## API

## `createContextProvider(contextName, initialValue, reducerFunction)` ⇒ `provider`

Creates a context and returns the provider.

| Param           | Type                           | Description                                                                                               |
| --------------- | ------------------------------ | --------------------------------------------------------------------------------------------------------- |
| contextName     | <code>string:required</code>   | Context name must be string without any spaces or special characters. **Examples**: `count, countContext` |
| initialValue    | <code>any:optional</code>      | Initial Value for the context                                                                             |
| reducerFunction | <code>function:optional</code> | Reducer function to update context value. Reducer function will receive two params `state` and `action`   |

<br/>

## `getContextValue(contextName)` ⇒ `contextValue`

Creates a context and returns the provider.

| Param       | Type                         | Description                                 |
| ----------- | ---------------------------- | ------------------------------------------- |
| contextName | <code>string:required</code> | Provide the context name to fetch its value |

<br/>

## `getContextAction(contextName)` ⇒ `contextSetterFunction`

Creates a context and returns the provider.

| Param       | Type                         | Description                                         |
| ----------- | ---------------------------- | --------------------------------------------------- |
| contextName | <code>string:required</code> | Provide the context name to get its setter function |

<br/>

## Usage

### To create new context

Use `createContextProvider` to create new context. It will return the provider for the created context

```jsx
import React, { Component } from 'react'
import { createContextProvider } from 'context-simplifier'

const App = () => {
  const CountProvider = createContextProvider('count', 0)

  return (
    <>
      <CountProvider>
        <Counter />
        <Layout>
          <CountDisplay />
        </Layout>
      </CountProvider>
    </>
  )
}
```

<hr/>

### To use the context value use `getContextValue`

```jsx
import React from 'react'
import { getContextValue } from 'context-simplifier'

const CountDisplay = () => {
  const count = getContextValue('count')

  return <div>{count}</div>
}

export default CountDisplay
```

<hr/>

### To get the setter for updating the context value

Use `getContextAction` to get the setter function which can be used to update the context value

```jsx
import React from 'react'
import { getContextAction } from 'context-simplifier'

const Counter = () => {
  const setCount = getContextAction('count')

  const increment = () => {
    setCount((oldValue) => oldValue + 1)
  }

  const decrement = () => {
    setCount((oldValue) => oldValue - 1)
  }

  return (
    <>
      <button onClick={increment}>Increment</button>
      <button onClick={decrement}>Decrement</button>
    </>
  )
}

export default Counter
```

## License

MIT © [Harshdand](https://github.com/Harshdand)

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