@quantajs/react
Version:
React integration for QuantaJS - A compact, scalable, and developer-friendly state management library for React applications.
141 lines (106 loc) ⢠3.46 kB
Markdown

React integration for QuantaJS - A compact, scalable, and developer-friendly state management library for React applications.
ā
**React Optimized** ā Built specifically for React applications
ā
**Performance First** ā Uses `useSyncExternalStore` for efficient updates
ā
**Type-Safe** ā Full TypeScript support with proper inference
ā
**Flexible** ā Multiple usage patterns (Provider, Direct, Selectors)
ā
**QuantaJS Core** ā Direct access to reactive, computed, and watch
## š¦ Installation
```sh
npm install @quantajs/react @quantajs/core
# or
yarn add @quantajs/react @quantajs/core
# or
pnpm add @quantajs/react @quantajs/core
```
## ā” Quick Start
### Basic Counter Example
```tsx
import React from 'react';
import { createStore, QuantaProvider, useStore } from '@quantajs/react';
// Create store
const counterStore = createStore('counter', {
state: () => ({ count: 0 }),
getters: {
doubleCount: (state) => state.count * 2,
},
actions: {
increment() {
this.count++;
},
decrement() {
this.count--;
},
},
});
// Component
function Counter() {
const store = useStore();
return (
<div>
<p>Count: {store.count}</p>
<p>Double: {store.doubleCount}</p>
<button onClick={() => store.increment()}>+</button>
<button onClick={() => store.decrement()}>-</button>
</div>
);
}
// App
function App() {
return (
<QuantaProvider store={counterStore}>
<Counter />
</QuantaProvider>
);
}
```
```tsx
import { useQuantaStore } from '@quantajs/react';
function CounterDisplay() {
// Only re-render when count changes
const count = useQuantaStore(counterStore, (store) => store.count);
return <p>Count: {count}</p>;
}
```
```tsx
import { useCreateStore } from '@quantajs/react';
function TodoComponent() {
const todoStore = useCreateStore(
'todos',
() => ({ todos: [] }),
undefined,
{
addTodo(text: string) {
this.todos.push({ id: Date.now(), text, done: false });
},
},
);
return (
<div>
<button onClick={() => todoStore.addTodo('New task')}>
Add Todo
</button>
<p>Todos: {todoStore.todos.length}</p>
</div>
);
}
```
- `useQuantaStore(store, selector?)` - Subscribe to store with optional selector
- `useStore(selector?)` - Access store from QuantaProvider context
- `useCreateStore(name, state, getters?, actions?)` - Create component-scoped store
- `<QuantaProvider store={store}>` - Provide store to child components
- `createStore`, `reactive`, `computed`, `watch` - Re-exported from @quantajs/core
This project is licensed under the MIT [License](https://github.com/quanta-js/quanta/blob/main/LICENSE) - see the LICENSE file for details.
We welcome contributions! Feel free to open issues, submit PRs, or suggest improvements.
If you find this library useful, consider giving it a ā star on [GitHub](https://github.com/quanta-js/quanta)!