# remode

> Based on the hooks to achieve a lightweight state management plan

Latest version **0.1.2** (published 2021-02-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install remode
pnpm add remode
yarn add remode
bun add remode
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2021-02-07 |
| First published | 2021-02-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 23.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | likun7981 |
| Maintainers | likun7981 |
| Keywords | state, redux, hook, management, react, model |

## Links

- npm: https://www.npmjs.com/package/remode
- Repository: https://github.com/likun7981/remode
- Homepage: https://github.com/likun7981/remode#readme
- Issues: https://github.com/likun7981/remode/issues
- npm.io page: https://npm.io/package/remode

## Dependencies (2)

- [deep-diff](https://npm.io/package/deep-diff.md) ^1.0.2
- [react-fast-compare](https://npm.io/package/react-fast-compare.md) ^3.2.0

## 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.1.2 (latest) — 2021-02-07
- 0.1.1 — 2021-02-07
- 0.1.0 — 2021-02-03
- 0.0.2-beta.3 — 2021-02-02
- 0.0.2-beta.2 — 2021-02-02
- 0.0.2-beta.1 — 2021-02-02
- 0.0.2-beta — 2021-02-02
- 0.0.2 — 2021-02-02
- 0.0.1 — 2021-02-02

## README

# remode
> **re**act **mode**l Use the hooks manage your global state

[![NPM version][npm]][npm-url] [![NPM downloads][npm-download]][npm-download-url]



[npm]: https://img.shields.io/npm/v/remode.svg
[npm-url]: https://www.npmjs.com/package/remode

[npm-download-url]: https://npmjs.com/package/remode
[npm-download]: https://img.shields.io/npm/dm/remode.svg

--------------------

## Features

- **lightweight** _Based on the React hooks to manage all of your state, no other dependencies_
- **faster** _Automatically track the change of the state, only after the state change to re-render the component_
- **easy** _Only three steps can be used directly, use only the react_
- **typescript** _Make you write the React code more easily_


## Install

```bash
$ npm i -S remode

## or

$ yarn add remode

```


## Use(Only three steps)

1. create the model
> A file is a model, logic clearer, more convenient maintenance

```ts
// src/models/user.ts
import { useState } from 'react';

// This is a generic hooks
const usePersistCallback = <T extends Function>(callback: T) => {
  const cbRef = useRef<Function>();

  cbRef.current = callback;

  return useCallback((...args) => cbRef.current!(...args), []);
};

function user() {
  const [userInfo, setUserInfo] = useState(null)
  // Function after persistent here, won't lead to additional rendering
  const login = usePersistCallback(() => {
    // login logic
  })
  return {
    login,
    userInfo
  }
}
```

2. init the model
> Each model has its own namespace

```ts
// src/models/index.ts

import initModels from 'remode';
import userModel from './user';


const Model = initModels({
  user: userModel,
  // otherNamespace: otherModel
})

export default Model
```

3. use the model

```ts
// src/index.tsx

import ReactDOM from 'react-dom'
import Model from './models';

// The root element mount Provider once
ReactDOM.render(
    <Model.Provider>
      // other element
    </Model.Provider>,
    document.getElementById('root')
)



// src/pages/login.tsx
import Model from '../models';

const LoginPage = () => {
  const { userInfo, login } = Model.useModel('user')
  // other logic
}
```

## Debug
You can use a logger to debug your state, add the code the file `src/models/index.ts`;
then you can see the output of the state in the console

```ts
if(process.env.NODE_ENV === 'development') {
  Model.subscribe(require('remode/logger'))
}
```

## Reference

- [@umi/plugin-model](https://github.com/umijs/plugins/tree/master/packages/plugin-model)

## LICENSE

[MIT][license-url]

[license-url]: https://github.com/likun7981/remode/blob/master/LICENSE
[license]: http://img.shields.io/npm/l/remode.svg

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