# react-ledom

> The State management library for React

Latest version **1.1.0** (published 2020-03-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-ledom
pnpm add react-ledom
yarn add react-ledom
bun add react-ledom
```

## 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.1.0 |
| Published | 2020-03-19 |
| First published | 2020-02-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Zetsin |
| Maintainers | zetsingithub |
| Keywords | react, model, state-management, react-hooks |

## Links

- npm: https://www.npmjs.com/package/react-ledom
- Homepage: https://github.com/zetsin/react-ledom
- npm.io page: https://npm.io/package/react-ledom

## 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.1.0 (latest) — 2020-03-19
- 1.0.5 — 2020-03-18
- 1.0.4 — 2020-03-18
- 1.0.3 — 2020-03-18
- 1.0.2 — 2020-03-17
- 1.0.1 — 2020-03-17
- 1.0.0 — 2020-02-23

## README

# react-ledom

The State management library for React

🎉 Support Both Class and Hooks Api

⚔ Fully TypeScript Support

```tsx
import { Model } from 'react-ledom'

// define state
interface TestState {
  value: number;
}

// define model
class TestModel extends Model<TestState> {
  state: TestState = {
    value: Date.now()
  }

  refresh = async () => {
    this.setState({
      value: Date.now()
    });
  }
}

// create store
const TestStore = new TestModel();

const App = () => {
  return (
    <Provider stores={[TestStore]}>
      <Test />
    </Provider>
  );
}

const Test = () => {
  const test = useModel(TestModel)
  return <div>
    <p>value: {test.state.value}</p>
    <button onClick={() => {
      test.refresh()
    }}>
      Refresh
    </button>
  </div>
}
```

---

## Quick Start
install package

```shell
npm install react-ledom
```

## Core Concept

### Model

Every model has customized properties and methods.

```tsx
import { Model } from "react-ledom";

export interface TestState {
  value: number;
}

export default class Test1Model extends Model<TestState> {
  state: TestState = {
    value: Date.now();
  }

  refresh = async () => {
    this.setState({
      value: Date.now()
    });
  }
}

export const Test1Store = new Test1Model();
```

### Provider

The [`Provider`](https://reactjs.org/docs/context.html#contextprovider) React component that allows consuming components to subscribe to context changes.

```tsx
import React, { FC } from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-ledom'

const Demo: FC = () => {
  return (
    <Provider stores={[store1, store2, ...]}>
      ...
    </Provider>
  )
};

render(<Demo/>, document.querySelector('#demo'))

```

### useModel

A helper for using [`useContext`](https://reactjs.org/docs/hooks-reference.html#usecontext) in functional components

```tsx
import React, { FC } from 'react'
import { useModel } from 'react-ledom';
import Test1Model from '../models/Test1Model';

const Test1: FC = () => {
  const test1 = useModel(Test1Model);

  return (
    <div>
      <h2>Test1</h2>
      <p>value: {test1.state.value}</p>
      <button onClick={() => {
        test1.refresh()
      }}>Refresh</button>
      <p>random( when Test1Model changed ): {Math.random()}</p>
    </div>
  )
}

export default Test1;
```

### Cousumer
The [`Cousumer`](https://reactjs.org/docs/context.html#contextconsumer) React component that subscribes to context changes.

```tsx
import React, { FC } from 'react'
import { Consumer } from 'react-ledom';
import Test1Model from '../models/Test1Model';
import Test2Model from '../models/Test2Model';

const Test1or2: FC = () => {

  return (
    <div>
      <Consumer model={Test1Model}>
        {test1 => (
          <>
            <h2>Test1</h2>
            <p>value: {test1.state.value}</p>
            <button onClick={() => {
              test1.refresh()
            }}>Refresh</button>
            <p>random( when Test1Model changed ): {Math.random()}</p>
          </>
        )}
      </Consumer>

      <Consumer model={Test2Model}>
        {test2 => (
          <>
            <h2>Test2</h2>
            <p>value: {test2.state.value}</p>
            <button onClick={() => {
              test2.refresh()
            }}>Refresh</button>
            <p>random( when Test2Model changed ): {Math.random()}</p>
          </>
        )}
      </Consumer>

      <p>random( once ): {Math.random()}</p>
    </div>
  )
}

export default Test1or2;
```

### Instances

You can use other model instance's values and methods directly.

```tsx
import { Model } from "react-ledom";
import { Test1Store } from "./Test1Model"
import { Test2Store } from "./Test2Model"

export interface TestProps {
  value: number;
}

export default class Test3Model extends Model<TestProps> {
  state: TestProps = {
    value: Date.now();
  }
  
  refresh = async () => {
    // await fetch("xxx", Test1Store.value)
    // await fetch("xxx", Test2Store.value)

    await Test1Store.refresh();
    await Test2Store.refresh();
  }
}

export const Test3Store = new Test3Model();
```

## Advance Concept

### State persistence

```tsx
import { Model } from "react-ledom";

export default abstract class Ledom<T> extends Model<T> {
  constructor() {
    super()

    Object.assign(this, JSON.parse(localStorage.getItem(this.constructor.name) ?? "{}"));
  }

  setState(data?: Partial<T>) {
    super.setState(data);

    localStorage.setItem(this.constructor.name, JSON.stringify(this));
  }
}

```

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