# react-truth

> A tiny state manager

Latest version **0.1.37** (published 2019-09-18) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.37 |
| Published | 2019-09-18 |
| First published | 2019-03-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 36.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Marcelo Zapaia |
| Maintainers | qzapaia |
| Keywords | react, state management, hooks, context, redux |

## Links

- npm: https://www.npmjs.com/package/react-truth
- Repository: https://github.com/zapaiamarce/react-truth
- Homepage: https://github.com/zapaiamarce/react-truth#readme
- Issues: https://github.com/zapaiamarce/react-truth/issues
- npm.io page: https://npm.io/package/react-truth

## Dependencies (2)

- [store](https://npm.io/package/store.md) ^2.0.12
- [lodash](https://npm.io/package/lodash.md) ^4.17.15

## 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.37 (latest) — 2019-09-18
- 0.1.36 — 2019-09-17
- 0.1.35 — 2019-09-17
- 0.1.34 — 2019-09-13
- 0.1.33 — 2019-09-03
- 0.1.32 — 2019-09-03
- 0.1.31 — 2019-08-23
- 0.1.30 — 2019-08-23
- 0.1.29 — 2019-08-21
- 0.1.28 — 2019-08-09
- 0.1.27 — 2019-08-09
- 0.1.26 — 2019-08-09
- 0.1.25 — 2019-08-09
- 0.1.24 — 2019-08-02
- 0.1.17 — 2019-07-18
- … 46 more at https://npm.io/package/react-truth/versions

## README

# Truth

A tiny state manager.

[![CircleCI](https://circleci.com/gh/zapaiamarce/react-truth.svg?style=shield)](https://circleci.com/gh/zapaiamarce/react-truth) [![npm](https://img.shields.io/npm/v/react-truth/latest.svg?color=brightgreen)](https://www.npmjs.com/package/react-truth)

## Setup

`yarn add react-truth`

## Basic use

```jsx
// state.js
import ReactTruth from "react-truth";

export class MyTruth extends ReactTruth {
  public async fetchData(){
    const res = await fetch("https://myapi.com/data");
    const data = await res.json();
    return {
      ...this.state,
      data
    }
  }
  // more actions ...
}

export default new MyTruth();
```

```jsx
// Component.js
import appState from "./state";

export default () => {
  const [state, actions] = appState.useState();

  return (
    <>
      <button onClick={actions.fetchUser}>Fetch Data</button>  
      <div>Data: {JSON.stringify(state.data)}</div> 
    </>
  );
};
```


## Advanced (Typescript)

```jsx
// state.tsx
import ReactTruth from "react-truth";

export class State {
  someValue: string = "initial from state class";
  anotherValue?: string;
}

export class MyTruth extends ReactTruth<State> {
  public async onLoad(): Promise<State> {
    return {
      ...this.state,
      someValue: "mounted"
    }
  }
  public async testAction(newValue): Promise<State> {
    // you can set the state any time you need
    await this.setState({
      ...this.state,
      testActionIsLoading: true
    });

    // you can end this actions setting a state using
    // this.setState as usual or just return a value

    return {
      ...this.state,
      someValue: newValue
    }
  }
}

const initialState = new State();
const settings = {
  persist: true,
  actionsStatus: true
};

export const myTruth = new MyTruth(initialState, settings);

export default myTruth;
```

```jsx
// Component.tsx
import appState from "./state";

export default () => {
  const [state, actions] = appState.useState();
  const handleClick = () => actions.testAction(Math.random());

  return (
    <div>
      <button onClick={handleClick}>
        Set a new random value {state.someValue}
      </button>
    </div>
  );
};
```

## Settings

#### persist:boolean = false

Persist the state in localStorage and recover it when the state starts.

#### persistanceKey:string = "persisted-state"

Used to name the localStorage item. default.

#### persistPick:string[] = null

Keys of persisted state members

#### actionsStatus:boolean = false

Generate automatic values in the state for actions status: state._status[actionName]
A **_status** member need to be declared in the state.

```jsx
// ...
export class State {
  data: object;

  // add this member to your State
  _status: any;
}

export class MyTruth extends ReactTruth<State> {
  public async apiCall(): Promise<State> {
    const res = await fetch("http://api.truth.com/v1/");
    const data = await res.json();
    return {
      ...this.state,
      data
    };
  }
}
// ...
```

```jsx
import state from "./state";
import { FIRED, FAILED, COMPLETED } from "react-truth";

export default () => {
  const [state, actions] = state.useState();
  const handleClick = () => actions.apiCall(Math.random());

  return (
    <div>
      <button onClick={handleClick}>
        {state._status.apiCall == FIRED ? (
          <span>The api call is happening</span>
        ) : state._status.apiCall == FAILED ? (
          <span>Something went wrong</span>
        ) : state._status.apiCall == COMPLETED ? (
          <span>Everything went ok!</span>
        ) : (
          <span>nothing happens yet</span>
        )}
      </button>
    </div>
  );
};
```

#### debug:boolean = false

Log react-truth internals to the console

## Truth Class

Any Truth instance has this methods to use or override

#### onLoad(): Promise<State>

Is executed right when the Truth instance is created

#### setState(newState): Promise<State>

Set the state with a new one. It´s async.

#### setStateRaw(newState): State

A sync state setter.

#### useState(pick:string[]): [State, actions]

React hook to plug a component to the state. A list of picked members of the state can be passed as unique param.

#### withState(Component: ReactComponent, stateResolver: (state)=> newState): ReactComponent
HOC to inject the state as props and the actions as **action** prop. 
A subset of the state can bi picked using a stateResolver



## Redux devtools integration

Check what is happening in the state in the **[redux devtools](https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=es)**.

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