0.1.37 • Published 5 years ago

react-truth v0.1.37

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Truth

A tiny state manager.

CircleCI npm

Setup

yarn add react-truth

Basic use

// 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();
// 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)

// 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;
// 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._statusactionName A _status member need to be declared in the state.

// ...
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
    };
  }
}
// ...
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

Is executed right when the Truth instance is created

setState(newState): Promise

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.

0.1.37

5 years ago

0.1.36

5 years ago

0.1.35

5 years ago

0.1.34

5 years ago

0.1.33

5 years ago

0.1.32

5 years ago

0.1.31

5 years ago

0.1.30

5 years ago

0.1.29

5 years ago

0.1.28

5 years ago

0.1.27

5 years ago

0.1.26

5 years ago

0.1.25

5 years ago

0.1.24

5 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago