# @lxsmnsyc/react-provider

> Flutter's Depedency Injection pattern for React

Latest version **0.5.7** (published 2019-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @lxsmnsyc/react-provider
pnpm add @lxsmnsyc/react-provider
yarn add @lxsmnsyc/react-provider
bun add @lxsmnsyc/react-provider
```

## 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.5.7 |
| Published | 2019-11-04 |
| First published | 2019-10-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=8 |
| Dependencies | 0 |
| Unpacked size | 370.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | LXSMNSYC |
| Maintainers | lxsmnsyc |

## Links

- npm: https://www.npmjs.com/package/@lxsmnsyc/react-provider
- Repository: https://github.com/LXSMNSYC/react-provider
- npm.io page: https://npm.io/package/@lxsmnsyc/react-provider

## Recent versions

- 0.5.7 (latest) — 2019-11-04
- 0.5.6 — 2019-10-30
- 0.5.5 — 2019-10-30
- 0.5.4 — 2019-10-30
- 0.5.3 — 2019-10-30
- 0.5.2 — 2019-10-30
- 0.5.1 — 2019-10-30
- 0.5.0 — 2019-10-30
- 0.4.0 — 2019-10-08
- 0.3.1 — 2019-10-08
- 0.3.0 — 2019-10-07
- 0.2.1 — 2019-10-04
- 0.1.1 — 2019-10-03
- 0.1.0 — 2019-10-03

## README

# react-provider

> Flutter&#x27;s Depedency Injection pattern for React

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

## Install

```bash
npm install --save @lxsmnsyc/react-provider
```

## Examples

### Basic

```tsx
import React from 'react'

import { Provider, Consumer } from 'react-provider'

export default function App() {
  return (
    <Provider of="A" value="Hello World">
      <Consumer of="A" builder={x => <h1>{x}</h1>} />
    </Provider>
  )
}
```

### Nested using filters

```tsx
import React from 'react'

import { Provider, Consumer } from 'react-provider'

export default function App() {
  return (
    <Provider value="Hello World">
      <Provider value="Hello Alexis">
        <Consumer
          of={x => typeof x === 'string'}
          builder={x => <h1>{x}</h1>}
        />
      </Provider>
      <Consumer
        of={x => typeof x === 'string'}
        builder={x => <h1>{x}</h1>}
      />
    </Provider>
  )
}
```

### Promise

```tsx
import React from 'react'

import { PromiseProvider, PromiseConsumer } from 'react-provider';

export default function App() {
  return (
    <PromiseProvider of="Test" value={Promise.resolve('Hello World')}>
      <PromiseConsumer
        of="Test"
        onDefault={() => <h1>Default</h1>}
        onSuccess={x => <h1>{x}</h1>}
        onFailure={x => <h1>{x.message}</h1>}
        onLoading={() => <h1>Loading...</h1>}
      />
    </PromiseProvider>
  )
}
```

### ChangeNotifier

```tsx
import React, { useCallback } from 'react'

import { useChangeNotifierProvider, ChangeNotifierProvider, ChangeNotifierConsumer, ChangeNotifier } from 'react-provider'

class CounterModel extends ChangeNotifier {
  constructor() {
    super();
    this.count = 0;
  }

  increment() {
    this.count++;
    this.notifyListeners();
  }

  decrement() {
    this.count--;
    this.notifyListeners();
  }
}

function Increment() {
  const notifier = useChangeNotifierProvider('Test', false);
  const onClick = useCallback(() => notifier.increment(), [ notifier ]);
  return (
    <button type="button" onClick={onClick}>Increment!</button>
  )
}

function Decrement() {
  const notifier = useChangeNotifierProvider('Test', false);
  const onClick = useCallback(() => notifier.decrement(), [ notifier ]);
  return (
    <button type="button" onClick={onClick}>Decrement!</button>
  )
}

export default function App() {
  return (
    <ChangeNotifierProvider of="Test" value={new CounterModel()}>
      <ChangeNotifierConsumer
        of="Test"
        builder={model => <h1>{model.count}</h1>}
      />
      <Increment />
      <Decrement />
    </ChangeNotifierProvider>
  )
}
```

### Composing multiple Providers

```tsx
import React from 'react'

import { MultiProvider, Consumer, Provider } from 'react-provider'

export default function App() {
  return (
    <MultiProvider
      providers={[
        <Provider of="A" value="Hello World" />,
        <Provider of="B" value="Hello Alexis" />,
        <Provider of="C" value="Hello Lyon" />,
      ]}
    >
      <Consumer of="A" builder={x => <h1>{x}</h1>} />
      <Consumer of="B" builder={x => <h1>{x}</h1>} />
      <Consumer of="C" builder={x => <h1>{x}</h1>} />
    </MultiProvider>
  )
}
```

## License

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

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