simpply
A simple state management library for React app, built on top of the Context & Hooks APIs.
Table of contents
Installation
Run npm i simpply
.
Defining the notion of a Storage Entity
A Storage Entity
in simpply
is just an object which has two properties - initialState
and effects
. It defines a state for a particular system resource (or entity) and how it behaves or changes over time.
Below you have an example of a User Storage Entity
that could be separated in a file called users.js
:
// This goes in `users.js`. /* * We are adding a new user (defined by the `payload` construct). * * E.g. for a first time add: * `state` would be the `initialState` (that is `[]`). * `payload` would be `John Doe`. */const addUser = ...state payload; const effects = ADD_USER: addUser; initialState: effects;
An effect
is a function that has the following signature: (state, payload)
. state
represents the current value of the Storage Entity
we apply the effect for. payload
represents the data passed through the system when a certain action will be dispatched (more on that later).
Why are Storage Entities important
simpply
provides an API so that all the Storage Entities
in the system are combined into a System Storage
. A System Storage
is, in the context of simpply
, just a fancy name that describes an object containing the system's state and all the effects associated with it (and that will change the state over time). The System Storage
is used to create the final store
(state + dispatch
) by employing the userReducer
hook in React.
Documentation
createSystemStorage
/** * @param * @returns */
Combines all the Storage Entities
in the app under the same umbrella. A Storage Entity
file must contain the following two exports:
// Can be any primitive type, except `undefined` and `Symbol` initialState: ... // Collection of effects effects: ...;
For details on what each property represents, check the above documentation for Storage Entity
.
E.g. of a User Storage Entity
implementation with an effect to add a new user, a Puppy Storage Entity
with an effect to delete a puppy and how they are combined via createSystemStorage
:
// In `users.js` initialState: effects: ...state payload ; // In `puppies.js` initialState: effects: ...state payload ; // Then in a separate file (e.g.: `systemStorage.js`), combine the two: ;; users puppies;
This function returns an object containing the global initial state of the system and all the effects associated with it. The end result of applying createSystemStorage
will then be used to create the application's main Provider
.
createProvider
/** * @param * @param * @return */
Creates the application's main Provider
component that serves the resulting store via Context API.
E.g. of using createProvider
:
// In index.js ;; const AppProvider = ; ReactDOM;
The options
object looks like this:
// [Boolean] Specifies if logging for the last triggered action, previous & current state is enabled. Defaults to `true` in `development` and `false` otherwise. logging: true // [Boolean] Specifies if `getState` method is attached to `window` for debugging purposes. Defaults to `true` in `development` and `false` otherwise. globallyDebugState: true
connect
/** * @param {Function | Null} mapStateToProps A function returning an object defining which slice of the global state will be injected in the wrapper component. If `mapStateToProps` is `null`, only `dispatch` will be injected. * @returns */
Creates a Higher Order Function (HOF) that can be later applied to a React component. The result of applying the function is a wrapper component that will have a slice of the global state automatically injected as well as the dispatch
function.
E.g. of using connect
for a PuppiesList component:
// In `PuppiesList.js` ; const PuppiesList = <ul> puppies </ul>; const mapStateToProps = /* * This is where we define that `PuppiesList` cares only about * the `puppies` property from the global state. */ puppies: statepuppies; mapStateToPropsPuppiesList;
The connect
function is similar to Redux's connect
functionality. However, it does not need a mapDispatchToProps
function. dispatch
is automatically injected in the connected component and simpply
encourages making use of it directly. Below you have an example of dispatching, as a result of a successful async call.
// In MyComponent.js const MyComponent = { ; return <div>/* Some markup here ... */</div>;};
Examples
For an example that uses simpply
, check out this repo.
Author
Name: Vlad Zelinschi
Email: the.reignn@gmail.com