Very minimal state management lib based on react hooks.
create a store and a hook by using sparx
// make a store , there could be multi store in on app or just on, your choice
import createSparkHooks from "sparx";
const {useSpark, sparkle, useSubscribeStoreChange_UNSTABLE, useStore} = createSparkHooks();
export { useSpark, sparkle };
create a state.js where your sparks live, component can import sparks from this module
// import the sparkle method from the store
import {sparkle} from 'store.js'
// use sparkle to make a spark
export const data_1 = sparkle({
key: 'string key',
value: {} // any data
})
create a component which consume the spark by using the useSpark hook which from the store
import {useSpark} from 'store.js'
import {data_1} from 'state.js'
const Comp1 = () => {
// by call the useSpark hook, you can get the data from the spark, and a updater to update the spark
// the same as useState
const [data1, setData1] = useSpark(data_1);
// ....
// your render logic
}
create a component comsume the same spark as the previous compoent, notice that, each of these two component updating the data by calling setData1 will trigger both component update/rerender
//
import {useSpark} from 'store.js'
import {data_1} from 'state.js'
const Comp1 = () => {
const [data1, setData1] = useSpark(data_1);
// ....
// your render logic
}
[] complete senarios [] support aync spark [] data serialize, unserilaize [] store initial state [] dynamic spark helper [] memorize [*] documentation