react-spit
Easy data handling in react.
A library with the advantages of react-redux but less boilerplate code.
Getting started
Create a basket for your data (CatContainer.jsx):
; const catEvent = 'cats'; catEvent;
This creates you an Event and a Container for your data. In this example is this an Array of cats.
The new SpitEvent([], 'cats')
creates an Event, this is used for handling new incoming data.The first param of the constructor is the initial Value, your container will have. The second is an identifier. This is mostly used for ServerSide rendering or for debugging causes. More on this later.
If you use hooks, you can simply use the useSpit hook (from version 1.1.0):
;; { const cats setCats = ; return cats;}
Or the old way with a render props container. To use it (Animals.jsx):
; <CatContainer> data </CatContainer>
So this is basically it. You can use this container everywhere you want to have your cat data. You can use the same container to set the data:
; <CatContainer> <button onClick= >Set cats</button> </CatContainer>
This will set your cat data globally. You have always the same data available. A single source of truth.
Any call of the set function will trigger your CatContaier to re-render.
Google Chrome Extension
Get your Google Chrome extension here: Chrome Web Store. With the Extension you can analyse the different Containers and their versions.
To enable the Chrome extension just add this snippet to your code:
; // Enable React Spit Chrome Extensionif typeof window !== undefined window__REACT_SPIT_DEV_TOOLS__ && window;
Advanced Usage
In most cases, setting any data is not as simple as the first use case. If you like to fetch any data for example its more complex.
Fetch data
The easiest way would be to just create a new function in a new file, import the cat event, set the data on fetch.then
and use this function
in useEffect or componentDidMount if you dont use hooks (fetchCatsAction.js):
; { const result = await ; const json = await result; catEvent;};
This could be called in useEffect:
; ... ;
Or any componentDidMount:
; ... { ;}
In fact that the Event triggers all the containers, everywhere you use a cat container, it will be updated. But this code example cant only used once and is not really good testable.
So i prefer to wrap the function to have a dynamic function (fetchCatsAction.js):
async { const result = await ; const json = await result; event;};
The connection between the action and the event can now be established in the view (Cats.jsx):
;; const fetchCats = ; ... { ;}