2.0.0 • Published 12 months ago

simple-context-state v2.0.0

Weekly downloads
81
License
ISC
Repository
-
Last release
12 months ago

Simple-Context state

Description

Enables quick setup and easy management of state in react applications. Provides an in-build Errors store and Pending store (stores being blocks of state) which interact with the stores you create. Uses Reacts Context API and does not rely on any third party dependencies.

Install

    npm install

Detailed guide

https://adamjduggan.github.io/demo-simple-context-state

API

MethodDescription
<SimpleProvider/>Provides state and actions to child components.
useSimpleState()Access actions and state from your stores.
useSimpleErrors()Access the in-built errors stores.
useSimplePending()Access the in-built pending stores. components.

Guide

1. Create your own stores as basic objects with a name (string), initialState (any data type), actions (object of functions) and asyncActions (object of asynchronous functions).

const TodosStore = {
    name: "todos",
    initialState: ["Buy milk", "Start running", "Phone a friend"],
    actions: {
        add: (state) => (payload) => {
          const newState = [...state, payload];
          return newState;
        }
    },
    asyncActions: {
        fetch: (state) => (payload) => async () => {
          const responce = await fetch(`https://...`);
          const newState = [...state, responce];
            return newState;
        },
    },
};

2. Wrap a React component with SimpleProvider and pass your stores to it as an array.

import { SimpleProvider } from "simple-context-state"";

// Example with one provider for whole app (index.js file)
ReactDOM.render(
        <SimpleProvider component={<App />} stores={[TodosStore, AnotherStore]} />,
        document.getElementById("root")
);

// Example of using multiple providers in larger applications (App.js file or another component)
function App(){
  return (
    <SimpleProvider component={<Todos />} stores={[TodosStore]} />
    <SimpleProvider component={<Food />} stores={[FruitStore, VegtableStore]} />
  )
}

3. Access state and actions from your stores in components.

Here todos is the array state.todos. todos_add() and todos_fetch() are the "add" and "fetch" actions created in the TodosStore. Stores may have actions using the same name so we access actions with storeName_actionName. The action errors_reset() is avalible globally and clears the errors store.

import { useSimpleState } from "../../simple-context-state"";

function TodosComponent(){

  const { todos, todos_add, todos_fetch, clear_errors } = useSimpleState();

  return (

    // todos is the state from the TodoStore
      {todos &amp;&amp; todos.map((t) =&gt; &lt;p&gt;{t}&lt;/p&gt;)}

    // Actions are accessed by storeName_actionName
    <button onClick={() => todos_add("Buy a bike")}>Add async</button>

    // Same for asyncActions
    <button onClick={() => todos_fetch()}>Get todo from API</button>

    // clear_errors is an in-built action which clears the errors store
    <button onClick={() => errors_clear()}>Clear Errors</button>
  );

}

4. Access the errors store and pending store with two simple hooks

Each SimpleProvider in your app exposes an errors store and pending store. asyncActions are automatially wrapped in a pending state and errors state so at anytime you can see whether your asynchronous actions are loading, have resolved or have failed. The useSimpleErrors() hook and useSimplePending() hook both recieve an array of strings. Each string can be either the name of a store or the name of a specific action in a store. If you pass the name of a store the hook will return any/all actions in that store which are pending (usePendingHook) or have failed (useErrorsHook). If the string is the name of an action the hooks will check if that action is in the pending or errors store respectively.

// Will get all/any errors from the errors store
const errors = useSimpleErrors();

// This will get any actions from the products store and/or auth store which are pending
const pending = useSimplePending("auth", "products");

// Checks the errors store for to see if either/both of these actions from the auth store have failed
const errors = useSimpleErrors("auth_login", "products_get");
2.0.0

12 months ago

1.9.61

3 years ago

0.1.0

3 years ago

0.1.1

3 years ago

1.9.60

3 years ago

1.9.59

3 years ago

1.9.58

3 years ago

1.9.57

3 years ago

1.9.56

3 years ago

1.9.55

3 years ago

1.9.54

3 years ago

1.9.53

3 years ago

1.9.52

3 years ago

1.9.51

3 years ago

1.9.50

3 years ago

1.9.49

3 years ago

1.9.48

3 years ago

1.9.47

3 years ago

1.9.46

3 years ago

1.9.45

3 years ago

1.9.44

3 years ago

1.9.42

3 years ago

1.9.41

3 years ago

1.9.40

3 years ago

1.9.39

3 years ago

1.9.38

3 years ago

1.9.37

3 years ago

1.9.36

3 years ago

1.9.35

3 years ago

1.9.34

3 years ago

1.9.32

3 years ago

1.9.31

3 years ago

1.9.30

3 years ago

1.9.29

3 years ago

1.9.28

3 years ago

1.9.27

3 years ago

1.9.26

3 years ago

1.9.25

3 years ago

1.9.24

3 years ago

1.9.23

3 years ago

1.9.22

3 years ago

1.9.21

3 years ago

1.9.20

3 years ago

1.9.19

3 years ago

1.9.18

3 years ago

1.9.17

3 years ago

1.9.16

3 years ago

1.9.15

3 years ago

1.9.14

3 years ago

1.9.13

3 years ago

1.9.12

3 years ago

1.9.11

3 years ago

1.9.10

3 years ago

1.9.9

3 years ago

1.9.8

3 years ago

1.9.7

3 years ago

1.9.6

3 years ago

1.9.4

3 years ago

1.9.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.5

3 years ago

1.7.4

3 years ago

1.7.3

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago