Skip to main content

react-logic

A simple logic management library for React

  • Component logic separated from the render cycle
  • True reactive state based on signals
  • Shared functionality with dependency injection
// Top Wikipedia articles for a chosen year in a specific day

class ItemsService {
data = fetchState((year = '2026') => getUrl(year));

items = computedState(() => this.data()?.items.at(0)?.articles ?? []);
}

class ItemsLogic {
service = inject(ItemsService);

filtered = computedState((q = '') => {
const gx = new RegExp(q, 'i');
return this.service.items().filter(a => gx.test(a.article));
});
}

const App = () => {
const l = useLogic(ItemsLogic);
return (
<div>
<select onChange={e => l.service.data.fetch(e.target.value)}>
{ YEARS.map(y => <option key={y}>{y}</option>) }
</select>
<input onChange={e => l.filtered(e.target.value)} />
<ul>
{ l.filtered().map(a => <li key={a.rank}>{a.article}</li>) }
</ul>
</div>
);
};