Legend-State

Legend State是一个超级快速和强大的状态库,支持fine-g...

README

Legend-State


Legend State是JavaScript应用程序的超级快速和强大的状态管理器,有两个主要目标:

1. 🦄 尽可能容易使用


没有样板,也没有动作、减速器、选择器、分派器、传奇、恶棍或史诗。可观察对象只是正常的对象,您可以倾听它们的变化。

  1. ```jsx
  2. // Create an observable object
  3. const state = observable({ settings: { theme: 'dark' } })

  4. // Just get and set
  5. state.settings.theme.get() === 'dark'
  6. state.settings.theme.set('light')

  7. // observe re-runs when accessed observables change
  8. observe(() => {
  9.     console.log(state.settings.theme.get())
  10. })

  11. // Observer components automatically track observables and re-render when they change
  12. const Component = observer(function Component() => {
  13.     const theme = state.settings.theme.get()

  14.     return <div>Theme: {theme}</div>
  15. })
  16. ```

2. ⚡️ 最快的React状态库


Legend State在几乎每一个指标上都胜过其他任何一个状态库,并且对数组进行了如此优化,甚至在交换基准测试上胜过了普通JS。只有3kb,并且样板代码大大减少,文件大小也会大大节省。


有关详细信息,请参阅文档 for more details。

安装


npm install @legendapp/state or yarn add @legendapp/state

实例


  1. ```jsx
  2. import { observable } from "@legendapp/state"
  3. import { observer } from "@legendapp/state/react";
  4. import { persistObservable } from "@legendapp/state/persist";

  5. // Create an observable object
  6. const state = observable({ settings: { theme: 'dark' } })

  7. // get() returns the raw data
  8. state.settings.theme.get() === 'dark'

  9. // observe re-runs when any observables change
  10. observe(() => {
  11.     console.log(state.settings.theme.get())
  12. })

  13. // Assign to state with set
  14. state.settings.theme.set('light')

  15. // Automatically persist state. Refresh this page to try it.
  16. persistObservable(state, { local: 'exampleState' })

  17. // Components re-render only when accessed observables change
  18. const Component = observer(function Component() {
  19.     const theme = state.settings.theme.get()
  20.     // state.settings.theme is automatically tracked for changes

  21.     const toggle = () => {
  22.         state.settings.theme.set(theme =>
  23.             theme === 'dark' ? 'light' : 'dark'
  24.         )
  25.     }

  26.     return (
  27.         <div
  28.             className={theme === 'dark' ? 'theme-dark' : 'theme-light'}
  29.         >
  30.             <div>Theme: {theme}</div>
  31.             <Button onClick={toggle}>
  32.                 Toggle theme
  33.             </Button>
  34.         </div>
  35.     )
  36. })
  37. ```

集锦


- ✨ Super easy to use - observables are normal objects
- ✨ No boilerplate
- ✨ Safe from 🔫 footguns
- ✨ Designed for maximum performance and scalability
- ✨ React components re-render only on changes
- ✨ Very strongly typed with TypeScript
- ✨ Persistence plugins for automatically saving/loading from storage
- ✨ State can be global or within components

Read more about why Legend-State might be right for you.

文档



社区


加入我们的Slack,加入Legend社区。

Road to 1.0


- [ ] Improve documentation
- [ ] An examples page
- [ ] Fix types for TypeScript strict mode

在进行中


- [ ] IndexedDB持久性插件
- [ ] Firebase实时数据库的远程持久性插件

👩‍⚖️ 许可证




Legend-State 由 Jay Meistrich with Legend and Bravely 创建并维护。

Legend      Bravely