Swear JS
Simple react state-manager
Wrap your application with StoreContext
import {createStore, StoreContext} from "swear-js";
function App() {
const store = createStore();
return (
<StoreContext.Provider value={store}>
...
</StoreContext.Provider>
);
}
Creating Swear
"Swear" is the name of your state particle.
- Default swear that stores numeric value of counter.
import {createSwear} from "swear-js";
const countSwear = createSwear('counter', 0, {
set: (mutate) => (payload: number) => {
mutate(payload);
}
});
- You can also pass function to
mutate
with previous value of your state.
import {createSwear} from "swear-js";
const countSwear = createSwear('counter', 0, {
set: (mutate) => (payload: number) => {
mutate(prev => prev + payload);
}
});
Usage
import {createSwear, useSwear} from "swear-js";
const countSwear = createSwear('counter', 0, {
set: (mutate) => (payload: number) => {
mutate(prev => prev + payload);
return "Test string";
}
});
const [value, {set}] = useSwear(countSwear);
const foo = () => {
set(10);
}
foo();
Operating with return values of actions
import {createSwear, useSwear} from "swear-js";
const countSwear = createSwear('counter', 0, {
set: (mutate) => (payload: number) => {
mutate(prev => prev + payload);
return "Test string";
}
});
const [value, {set}] = useSwear(countSwear);
const foo = () => {
const response = set(10);
console.log(response);
}
foo();
Contributing
Project repository: https://gitlab.com/soundsnick/swear-js