React-ish is a lightweight reactive library designed for experimenting with state management and reactivity. It provides a simple API for creating reactive states, computed values, and effects.
- State: Create reactive state objects that notify subscribers when updated.
- Computed: Derive values from reactive states that automatically update when dependencies change.
- Effect: Run side effects in response to state changes.
- Readonly: Expose state as read-only to prevent external modifications.
- Watch: Observe changes in state or computed values and react to them.
Clone the repository and install dependencies using pnpm:
pnpm installimport { state, computed, effect } from "./lib";
const count = state(0);
const computedCount = computed(() => count.value * 2);
effect(() => {
console.log("Count is", count.value);
});
effect(() => {
console.log("Computed count is", computedCount.value);
});
setInterval(() => {
count.set(count.value + 1);
}, 2000);Creates a reactive state object.
- Parameters:
initialValue- The initial value of the state. - Returns: A state object with
value(getter) andset(newValue)(setter).
Creates a computed state derived from other reactive states.
- Parameters:
callback- A function that returns the computed value. - Returns: A state object with
value(getter).
Creates a readonly version of a state object.
- Parameters:
state- The state object to make readonly. - Returns: A readonly state object with
value(getter).
Observes changes in a state or computed value and executes a callback.
- Parameters:
source: A function that returns the value to watch.callback: A function that receives the new and old values.options: An optional object with options for the watcher.skip: A boolean indicating whether to skip the internal effect execution.
Registers a side effect that runs whenever its dependencies change.
- Parameters:
callback: A function that optionally returns a cleanup function.options: An optional object with options for the effect.skip: A boolean indicating whether to skip the effect's execution.
pnpm run build: Compiles the TypeScript code usingtsup.pnpm run start: Runs the compiled code.pnpm run dev:tsc: Watches for TypeScript changes and recompiles.pnpm run dev:node: Watches for changes and restarts the Node.js process.pnpm run dev: Runs bothdev:tscanddev:node.pnpm run test: Runs tests withvitestand generates a coverage report.pnpm run test:ci: Runs tests with verbose reporting and coverage for CI environments.pnpm run test:watch: Runs tests in watch mode.pnpm run lint: Checks for linting errors usingeslint.pnpm run format: Formats the code using Prettier.pnpm run format:check: Checks if the code is formatted correctly.pnpm run clean: Removes thedistdirectory.pnpm run analyze: Analyzes the bundle size usingsource-map-explorer.
This project is licensed under the ISC License.
Created by Berenjena 🍆