GithubHelp home page GithubHelp logo

dourajs / doura Goto Github PK

View Code? Open in Web Editor NEW
31.0 1.0 4.0 2.17 MB

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

Home Page: https://dourajs.github.io/doura/

JavaScript 7.00% Shell 0.04% TypeScript 92.97%
doura immutable react reactive state-management

doura's Introduction

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:

doura's People

Contributors

liximomo avatar wangjinyang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.