Skip to content

:shipit: 基于 React Hooks 的数据流方案

License

Notifications You must be signed in to change notification settings

simpleler/icestore

 
 

Repository files navigation

Icestore

基于 React Hooks 的数据流方案。

NPM version Package Quality build status Test coverage NPM downloads Known Vulnerabilities David deps

安装

npm install @ice/store --save

快速上手

声明 Store:

// src/stores/todos.js
export default {
  dataSource: [],
  async refresh() {
    this.dataSource = await (await fetch(/* api */)).json();
  },
  async action() {
    // ...
  },
  actionSync() {
    // ...
  },
};

注册 Store:

// src/stores/index.js
import todos from './todos';
import Icestore from '@ice/store';

const icestore = new Icestore();
icestore.registerStore('todos', todos);

export default icestore;

在 View 中使用:

// src/index.js
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';
import stores from './stores';

function Todo() {
  const todos = stores.useStore('todos');
  const { dataSource } = todos;

  useEffect(() => {
    todos.refresh();
  }, []);

  return (
    <div>
      <h2>Todo</h2>
      <ul>
        {dataSource.map(({ name }) => (
          <li>{name}</li>
        ))}
      </ul>
    </div>
  );
};

ReactDOM.render(
  <Todo />,
  document.body
);

示例

参考

About

:shipit: 基于 React Hooks 的数据流方案

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 99.0%
  • JavaScript 1.0%