1.0.72 • Published 3 years ago

@fishbot/store-context v1.0.72

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
3 years ago

What is StoreContext?

StoreContext is based on the the Observer pattern which is used in numerous libraries: Redux, Zustand, Recoil, etc. => StoreContext can replace Redux, Zustand, Recoil completely with the built-in React features without installing any external libraries.

Example of StoreContext: https://codesandbox.io/s/context-selector-demo-ifnqr5?file=/src/StoreContext.js

Usage in component

function SimpleComponent() {
  const programId = 'id1';
  const program = useStore((store) => store.programs[programId]);
  const actions = useActions();

  useEffect(() => {
    actions.programs.fetch({ docId: programId });
  }, [actions, programId]);

  return <div>{program?.name}</div>;
}

More configs

Log functions

By default

Default and additional callbacks

  • By default, onDone, onError, and onFinal are the default callbacks for each action, e.g.
    actions.programs.fetch({ docId: programId }, {
      onDone: (doc) => {},
      onError: (error) => {},
      onFinal: () => {},
    });
  • Additional callbacks can be passed to each action to the second param, e.g.

    actions.programs.fetch({ docId: programId }, {
      onCustomCallback: (payload) => {},
      onAnotherCustomCallback: (anotherPayload) => {},
    });

    and then use it in the action definition, e.g.

    const fetch = async ({ docId }, { dispatch, onCustomCallback, onAnotherCustomCallback }) => {
      // do something, e.g. call to backend
      const doc = await callBackend({ docId });
    
      // dispatch something
      dispatch({
        type: types.update,
        docId,
        doc,
      })
    
      // then execute callbacks
      if (someCondition) {
        onCustomCallback(someData);
      }
      if (someAnotherCondition) {
        onCustomCallback(someAnotherData);
      }
    }
1.0.72

3 years ago

1.0.71

3 years ago

1.0.48

3 years ago

1.0.47

3 years ago

1.0.49

3 years ago

1.0.51

3 years ago

1.0.53

3 years ago

1.0.52

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.40

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.45

3 years ago

1.0.29

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.37

3 years ago

1.0.34

3 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.28

3 years ago

1.0.27

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.0

4 years ago