Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/ doura Public

The simple, intuitive and reactive state manager you've been waiting for.

Notifications You must be signed in to change notification settings

dourajs/doura

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

134 Commits

Repository files navigation

doura

Doura is a decentralized state management solution based on the concept of model. It's very simple and intuitive.

  • πŸ”‘ 100% TypeScript Support
  • βš›οΈ Reactive and Immutable
  • πŸ”— Models are organized in a decentralized way

Installation

Install with npm:

npm install doura

Install with yarn

yarn add doura

Usage

Define Model

import { defineModel } from 'doura'
import { useModel } from 'react-doura'
const todoModel = defineModel({
 state: {
 todos: [
 {
 id: 0,
 text: 'read books',
 isFinished: true,
 },
 ],
 /** @type {'all' | 'unfinished'} */
 filter: 'all',
 },
 views: {
 unfinishedTodos() {
 // autocompletion! ✨
 return this.todos.filter((todo) => !todo.isFinished)
 },
 filteredTodos() {
 if (this.filter === 'unfinished') {
 return this.unfinishedTodos
 }
 return this.todos
 },
 },
 actions: {
 // any amount of arguments, return a promise or not
 setFilter(filter) {
 // you can directly mutate the state
 this.filter = filter
 },
 // action can be asynchronous
 async getTodos() {
 this.todos = await fetchTodos('httpds://api.example.com/todos')
 }
 },
})

Bind to React Components

import { useModel } from 'react-doura'
export function TodoApp() {
 // type of `filteredTodos` and `setFilter` are inferred automatically
 const { filteredTodos, setFilter } = useModel(todoModel)
 return (
 <div>
 <div>
 <input
 type="checkbox"
 id="filter"
 onClick={(event) =>
 setFilter(event.target.checked ? 'unfinished' : 'all')
 }
 />
 <label htmlFor="filter">Only show unfinished</label>
 </div>
 <ul>
 {filteredTodos.map((todo) => (
 <li key={todo.id}>
 <input type="checkbox" checked={todo.isFinished} />
 {todo.text}
 </li>
 ))}
 </ul>
 </div>
 )
}

Credits

Doura is greatly inspired by following excellent projects:

About

The simple, intuitive and reactive state manager you've been waiting for.

Topics

Resources

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 2

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /