0flux 中文文档教程

发布于 9年前 浏览 21 更新于 3年前

Ø Flux

一个小型的类型化状态管理库。

例子:

'use strict';

import * as Flux from '0flux';

interface TodoAction {
  name: string;
  id: number;
}

const container = new Flux();

const dispatcher = {
  addTodo: container.action<TodoAction>(),
  removeTodo: container.action<TodoAction>(),
};

interface State {
  todos: TodoAction[];
}

const todoState = container.state<State>((getState, setState) => {
  dispatcher.addTodo.bind((todoAction) => {
    setState({
      todos: getState().todos.concat([ todoAction ])
    });
  });

  dispatcher.removeTodo.bind((todoAction) => {
    setState({
      todos: getState().todos.filter((item) => {
        return item.id !== todoAction.id;
      })
    });
  });

  return {
    todos: []
  };
});

const julia = dispatcher.addTodo.dispatch({
  id: 0,
  name: 'julia'
});

const clone = dispatcher.addTodo.dispatch({
  id: 1,
  name: 'evil julia clone'
});

console.log('evil clone detected:');
console.log(todoState.current);

dispatcher.removeTodo.dispatch(clone);

console.log('evil clone eliminated:');
console.log(todoState.current);

Ø Flux

A small, typed state management library.

Example:

'use strict';

import * as Flux from '0flux';

interface TodoAction {
  name: string;
  id: number;
}

const container = new Flux();

const dispatcher = {
  addTodo: container.action<TodoAction>(),
  removeTodo: container.action<TodoAction>(),
};

interface State {
  todos: TodoAction[];
}

const todoState = container.state<State>((getState, setState) => {
  dispatcher.addTodo.bind((todoAction) => {
    setState({
      todos: getState().todos.concat([ todoAction ])
    });
  });

  dispatcher.removeTodo.bind((todoAction) => {
    setState({
      todos: getState().todos.filter((item) => {
        return item.id !== todoAction.id;
      })
    });
  });

  return {
    todos: []
  };
});

const julia = dispatcher.addTodo.dispatch({
  id: 0,
  name: 'julia'
});

const clone = dispatcher.addTodo.dispatch({
  id: 1,
  name: 'evil julia clone'
});

console.log('evil clone detected:');
console.log(todoState.current);

dispatcher.removeTodo.dispatch(clone);

console.log('evil clone eliminated:');
console.log(todoState.current);
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文