Code Monkey home page Code Monkey logo

akita-todo-react's Introduction

React Facades + RxJS

This repository provides tutorials to demonstrate the use of great state management using RxJS, Facades, and Akita.

image

Click here for Live Demo


Introduction

Powerful state management is easily supported... with super clean views. With the custom hook + Facade architecture handles all the complexity of state management, data pushing to views, and view change detections.

Unlike Redux - where developers have to dispatch actions - the TodosFacade API is called directly from the view component event handlers.

This approach radically simplifies the code complexity and reduces cruft.




Blog Post

image

Developers are encouraged to read the full-tutorial blog post: RxJS Facades in React




Using custom hook useTodosHook

export const TodosPage: React.FC = () => {
  const [filter, todos, facade] = useTodosHook();
  const history = facade.history;

  return (
    <div className="todoPage">
      <div className="toolbar">
        <Filters onChange={val => facade.updateFilter(val)} selectedFilter={filter} />
        <TodoForm onAdd={(item) => facade.addTodo(item)} />
        
        <div className="history">
          <button onClick={() => history.undo()} disabled={!history.hasPast}>   Undo </button>
          <button onClick={() => history.redo()} disabled={!history.hasFuture}> Redo </button>
        </div>
      </div>

      
      <TodoList
        todos={todos}
        onToggle={(item) => facade.toggleComplete(item)}
        onDelete={(item) => facade.deleteTodo(item)}
      />
    </div>
  );
};

Implement Custom Hooks + Observables

With a custom facade TodosFacade and hook useObservable(), implementing a Todo hook is trivial.

Notice the use of Akita state management is hidden inside the TodosFacade implementation

import { useObservable } from '@mindspace-io/utils';
import { facade, TodosFacade } from './todos.facade';
import { VISIBILITY_FILTER as v, Todo} from './todo.model';

export type TodoHookTuple = [string, Todo[], TodosFacade ];

export function useTodosHook(): TodoHookTuple {
  const [filter] = useObservable(facade.filter$, v.SHOW_ALL);
  const [todos] = useObservable(facade.todos$, []);

  return [filter, todos, facade];
}

Implement the Facade with Akita

Akita will manage state for Todos, immutable 1-way data flows, and data pushing via stream queries.

export class TodosFacade {
  readonly filter$ = this.query.filter$;
  readonly todos$ = this.query.visibleTodos$;  
  readonly history = new StateHistoryPlugin(this.todosQuery);

  constructor(private store: TodosStore, private query: TodosQuery) { }

  addTodo(text: string)        { this.store.add( createTodo(text)); }
  deleteTodo({id}: Todo)       { this.store.remove(id); }
  updateFilter(filter: number) { this.store.update({filter}) }  
  toggleComplete({ id }: Todo) { this.store.update(id, entity => {
    return { completed: !entity.completed };
  ))};
}

export const facade = new TodosFacade(todosStore, todosQuery);

Enforcing immutable data with ImmerJS

Immer.js will protect all state data from external, direct mutations. Using the produce(...) function the facade can easily mutate a draft version:

import { produce } from 'immer';

export class TodosFacade {

  updateFilter(filter: VISIBILITY_FILTER) {
    this.store.update( produce((draft:TodosState) => {
      draft.filter = filter;
    }));
  }

}

akita-todo-react's People

Contributors

thomasburleson avatar

Watchers

James Cloos 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.