项目作者: qq865738120

项目描述 :
🖖A React Hooks tool library, it is also a lightweight state management library.
高级语言: JavaScript
项目地址: git://github.com/qq865738120/react-neat.git
创建时间: 2019-12-07T15:16:11Z
项目社区:https://github.com/qq865738120/react-neat

开源协议:MIT License

下载


React Neat

GitHub license npm version

react-neat 是一个 React Hooks 工具库,同时它也是一个轻量级的状态管理库。

安装

你可以选择使用 npm 或者 yarn 安装

  • 使用 npm:npm install react-neat
  • 使用 yarn:yarn add react-neat

快速入门

只需简单的几步就可以获得一个功能完善的状态管理库,跟这下面的步骤开始吧。

tips:目前暂支持 hook 写法,因此需要使用>=16.8.0 的 React

  • 在项目根组件中注入状态库:

    1. // index.tsx
    2. import React from 'react';
    3. import App from './App';
    4. import { Provider, getStore, useStore } from 'react-neat'; // 引入
    5. const sleep = async t => new Promise(resolve => setTimeout(resolve, t));
    6. // actions用来操作状态库中的state,同是它支持异步操作。
    7. const actions = {
    8. // 同步action
    9. increment(state) {
    10. return { count: state.count + 1 };
    11. },
    12. // 异步action
    13. async decrement(state) {
    14. await sleep(2000);
    15. return { count: state.count - 1 };
    16. },
    17. // 传参
    18. setName(state, value) {
    19. return {name: value }
    20. }
    21. };
    22. export default function Index() {
    23. const userStore = getStore('user', actions); // 创建user store
    24. const storeReducer = useStore(userStore, { count: 0, name: 'my name' }); // 获取StoreReducer,它是操作store的核心
    25. // 通过Provider组件将storeReducer注入到根组件中,同时你还需要将userStore注入进去。
    26. return (
    27. <Provider value={storeReducer} store={userStore}>
    28. <App></App>
    29. </Provider>
    30. );
    31. }
  • 消费 store:

    1. // App.tsx
    2. import React from "react";
    3. import { getStore, useStoreContext } from "react-neat"; // 引入
    4. export default function App() {
    5. const { state, actions } = useStoreContext<any, any>(getStore("user")); // 获取userStore的StoreReducer,它提供了state以及actions用户获取以及操作状态。
    6. return (
    7. <section>
    8. <h2>login page</h2>
    9. <p>
    10. count: {state.count}, name: {state.name}
    11. </p>
    12. <button onClick={() => actions.increment()}>increment</button>
    13. <button onClick={() => actions.decrement()}>decrement</button>
    14. <button onClick={() => actions.setName('Bob')}>set name</button>
    15. </section>
    16. );
    17. }

    点击这里可以在线体验实例

进阶指南

通常我们可能需要创建多个store,为此提供了Providers组件可以方便的同时注入多个store

  • 在项目根组件中注入状态库:

    ```tsx
    // index.tsx
    import React from ‘react’;
    import ReactDOM from “react-dom”;
    import App from ‘./App’;
    import { Providers, getStore, useStore } from ‘react-neat’; // 引入
    import userActions, { userInitState } from ‘./actions/userActions’
    import bookActions, { bookInitState } from ‘./actions/bookActions’

    export default function Index() {
    // 创建user store
    const userStore = getStore(‘user’, userActions);
    // 创建book store
    const bookStore = getStore(‘book’, bookActions);
    // 获取StoreReducer,它是操作store的核心
    const userStoreReducer = useStore(userStore, userInitState);
    const bookStoreReducer = useStore(bookStore, bookInitState);

  1. // 通过Providers组件将storeReducer注入到根组件中,同时你还需要将userStore注入。
  2. // 与Provider组件不同的是,Providers可以同时注入多个store。
  3. return (
  4. <Providers values={[userStoreReducer, bookStoreReducer]} stores={[userStore,bookStore]}>
  5. <App></App>
  6. </Providers>
  7. );

}

ReactDOM.render(, document.getElementById(‘root’));

  1. ```tsx
  2. // actions/userActions
  3. const sleep = async t => new Promise(resolve => setTimeout(resolve, t));
  4. // actions用来操作状态库中的state,同是它支持异步操作。
  5. const userActions = {
  6. // 同步action
  7. increment(state) {
  8. return { count: state.count + 1 };
  9. },
  10. // 异步action
  11. async decrement(state) {
  12. let count = 0;
  13. await sleep(2000);
  14. return { count: state.count - 1 };
  15. },
  16. // 传参
  17. setName(state, value) {
  18. return {name: value }
  19. }
  20. };
  21. export default userActions
  22. // 状态初始化
  23. export const userInitState = { count: 0, name: 'Toney' }
  1. // actions/bookActions
  2. // actions用来操作状态库中的state,同是它支持异步操作。
  3. const bookActions = {
  4. setName(state, value) {
  5. return { name: value }
  6. },
  7. setAuthor(state, value) {
  8. return { author: value }
  9. }
  10. };
  11. export default bookActions
  12. // 状态初始化
  13. export const bookInitState = { name: 'Natural', author: 'Toney' }
  • 消费 store:

    1. // App.tsx
    2. import React from "react";
    3. import { getStore, useStoreContext } from "react-neat"; // 引入
    4. export default function App() {
    5. // 获取userStore的StoreReducer,它提供了state以及actions用户获取以及操作状态。
    6. const userStore = useStoreContext<any, any>(getStore("user"));
    7. const bookStore = useStoreContext<any, any>(getStore("book"));
    8. return (
    9. <section>
    10. <h2>App page</h2>
    11. <h3>people</h3>
    12. <p>
    13. count: {userStore.state.count}, name: {userStore.state.name}
    14. </p>
    15. <button onClick={() => userStore.actions.increment()}>increment</button>
    16. <button onClick={() => userStore.actions.decrement()}>decrement</button>
    17. <button onClick={() => userStore.actions.setName('Bob')}>set name</button>
    18. <h3>book</h3>
    19. <p>
    20. name: {bookStore.state.name}, author: {bookStore.state.author}
    21. </p>
    22. <button onClick={() => bookStore.actions.setName('Brief History of Time: from the Big Bang to Black Holes')}>set name</button>
    23. <button onClick={() => bookStore.actions.setAuthor('Stephen William Hawking')}>decrement</button>
    24. </section>
    25. );
    26. }

    点击这里可以在线体验实例

执照

MIT
Copyright (c) 2019-present, code_xia