tidux
v0.0.6
Published
Fast, small state management lib for React
Downloads
12
Maintainers
Readme
tidux
Fast, small state management lib for React
Features
- No store needed
- No Provider needed
- No reducer needed
- No action types needed
- Fluently state mutating, just assign new values to writable variables
- Simple API: subscribe, dispatch, useSelector
- Handling future action dispatching easily
- Support cancellable async dispatching
- Best for code splitting
Basic Counter
import React from "react";
import { dispatch, useSelector } from "tidux";
let $count = 0;
const Increase = () => $count++;
const App = () => {
const count = useSelector(() => $count);
return (
<>
<h1>{count}</h1>
<button onClick={() => dispatch(Increase)}>Increase</button>
</>
);
};
Async Counter
import React from "react";
import { dispatch, useSelector, delay } from "tidux";
let $count = 0;
const Increase = async () => {
await delay(1000);
$count++;
};
const App = () => {
const count = useSelector(() => $count);
return (
<>
<h1>{count}</h1>
<button onClick={() => dispatch(Increase)}>Increase</button>
</>
);
};
Calling another action inside current action (unsafe way)
import React from "react";
import { dispatch, useSelector, delay } from "tidux";
let $count = 0;
const Increase = () => $count++;
const IncreaseAsync = async () => {
await delay(1000);
Increase();
};
const App = () => {
const count = useSelector(() => $count);
return (
<>
<h1>{count}</h1>
<button onClick={() => dispatch(Increase)}>Increase</button>
<button onClick={() => dispatch(IncreaseAsync)}>Increase Async</button>
</>
);
};
Calling another action inside current action (safe way)
import React from "react";
import { dispatch, useSelector } from "tidux";
let $count = 0;
const Increase = () => $count++;
const IncreaseAsync = async (payload, { dispatch, delay }) => {
await delay(1000);
dispatch(Increase);
};
const App = () => {
const count = useSelector(() => $count);
return (
<>
<h1>{count}</h1>
<button onClick={() => dispatch(Increase)}>Increase</button>
<button onClick={() => dispatch(IncreaseAsync)}>Increase Async</button>
</>
);
};