chenjiawei.inizio adc4f8a9e8 chore: upgrade eslint (#1044) 6 days ago
..
__tests__ 9383fe33f6 fix(reactive): enhance the type inference capability of the ReactiveState.get and set (#796) 4 months ago
src 9383fe33f6 fix(reactive): enhance the type inference capability of the ReactiveState.get and set (#796) 4 months ago
README.md d7bdf8a078 feat: init flowgram.ai 10 months ago
eslint.config.js adc4f8a9e8 chore: upgrade eslint (#1044) 6 days ago
package.json adc4f8a9e8 chore: upgrade eslint (#1044) 6 days ago
tsconfig.json d7bdf8a078 feat: init flowgram.ai 10 months ago
vitest.config.ts cbefaa54fb chore: add license header (#432) 6 months ago

README.md

Reactive

Usage

创建响应式数据并做依赖追踪


import { ReactiveState, Tracker } from '@flowgram.ai/reactive'

// 创建 数据
const reactiveState = new ReactiveState<{ a: number, b: number }>({ a: 0, b: 0 })

// 监听函数
const result = Tracker.autorun(() => {
  console.log('run: ', reactiveState.value, reactiveState.value.a)
})

// 更新字典数据 a 会自动执行上边的 autorun
reactiveState.value.a = 1

// 更新数据 b 则不会执行,因为 autorun 函数里没有依赖
reactiveState.value.b = 1

react 中使用


import { useReactiveState, observe } from '@flowgram.ai/reactive'

const SomeComp = ({ state }) => {
  return <div>{state.a}</div>
}

function App() {
  const state = useReactiveState<{ a: number, b: number }>({ a: 0, b: 0 });
  useEffect(() => {
    // 触发 SompeComp 更新
    state.value.a = 1
    // 不触发 SompeComp 更新
    state.value.b = 1
  })
  return <SomeComp state={{state}} />
}