Migrating

0.21 to 0.22

Local Storage is no longer the default persistence

This was changed to reduce build size for those who don't use it. If you want to use Local Storage, configure it at the beginning of your app:

import { configureObservablePersistence } from '@legendapp/state/persist'
import { ObservablePersistLocalStorage } from '@legendapp/state/local-storage'

configureObservablePersistence({
    persistLocal: ObservablePersistLocalStorage
})

Moved persist plugins to /persist-plugins

Update your import paths:

import { ObservablePersistLocalStorage } from '@legendapp/state/persist-plugins/local-storage'
import { ObservablePersistMMKV } from '@legendapp/state/persist-plugins/mmkv'

when is not triggered by empty or []

If you wanted when to be triggered by those, you can update it to use a selector to return a boolean like:

const obs = {}
when(() => !!obs, () => {...})

0.20 to 0.21

Changed onChange callback

The extra paremeters in the onChange callback have changed to include an array of of the changes, fixing a bug where it was only showing the latest child's change when changing multiple children while batching. This likely won't affect many of you as it's mostly intended for internal use and persistence plugins.

Renamed React components from legend to Legend

We had originally used lower casing to match html elements, but in practice it did not autocomplete well and felt wrong. So just rename to uppercase, for example from <legend.div /> to <Legend.div />.

0.19 to 0.20

Removed deprecated automatic observing

The automatic observing from 0.18 was deprecated in 0.19 and is now removed. See Deprecated automatic observing.

observe and useObserve

As observe has gotten more and more powerful, it outgrew modifying behavior based on the return value, so it now has an event parameter to control canceling listening and a cleanup function.

  • If you were returning false to cancel observing, you can now use e.cancel = true.
  • If you were returning a cleanup function you can use e.onCleanup = () => ....
  • It also adds a num param to know how many times it's run and a previous param to compare to the previous value.
observe((e) => {
    // Cancel observing any future changes
    e.cancel = true

    // A cleanup function
    e.cleanup = () => ...
})

Renamed event dispatch to fire

Just change evt.dispatch() to evt.fire() and all is good 👍.

0.18 to 0.19

Deprecated automatic observing

We are deprecating the automatic observing that depended on hooking into React's internals. Components will no longer track observables automatically, but you can easily it per component in a few ways:

  • Wrap components in observer to make them track automatically
  • Wrap observable access in useSelector to return a value and track automatically.
  • Render observables directly into JSX.

So tracking observables in React can look like this now:

import { observer } from "@legendapp/state/react"

const Component = observer(function Component() {
    const value = observable.get();
    // This tracks because it's inside an observer
})

or

import { useSelector } from "@legendapp/state/react"

function Component() {
    // Track the value of an observable
    const value = useSelector(observable)

    // Track the return value of a function
    const isSelected = useSelector(() => id === state.selected.get())
}

See the React guide for how we suggest setting up your components now.

Rendering observables directly still works though, and enableLegendStateReact() still enables that.

You can still enable the previous behavior for now with enableLegendStateReact({ autoTrackingDEPRECATED: true }) while you migrate to using observer or useSelector. That option will be removed before we reach 1.0.

Why

  • It doesn't actually work. We thought this method would be safe to use because it was inspired by Preact Signals, but as we've integrated Legend-State into more environments we found significant edge cases that seem to be unfixable and suggest that the whole concept is just unworkable.
  • The React team has asked us not to do it and made it clear that it is likely to break in a future version of React.
  • As Legend-State has evolved, the ideal way of using it has shifted towards fine-grained reactivity where components render minimally or only once, and we were actually specifically opting out of auto-tracking more often than not. So in the interest of pursuing the render-once ideal, we think it's actually generally better to use the reactivity components or opt-in to tracking.
  • We don't want to distract from the core mission of Legend-State with an unreliable and unstable core.

Bindable components deprecated

We now have a more general purpose way of making reactive props that can also be used for two-way binding for inputs. So change:

<Bindable.input bind={observable} />

to

import { Legend } from "@legendapp/state/react-components"

<legend.input value$={observable} />

See reactive props for more detauls.

value is no longer exposed

Primitives no longer have a value that you could access and modify. We had previously removed that from the documentation and it is now removed from the code. You can just get() and set() as you would any other observable. It turned out to cause more bugs than it was worth and made the TypeScript types overly complex.

Removed get(false)

Use peek() instead.

0.17 to 0.18

The tracing functions are renamed to use* to be inline with hooks:

  • useTraceListeners
  • useTraceUpdates
  • useVerifyNotTracking
  • useVerifyOneRender (new)

0.16 to 0.17

Primitives are now returned as observables

Observables previously tried to be clever by returning primitives directly, which was great in making it easy to work with state directly. But especially as the goal has moved more towards fine-grained reactivity, the balance shifted towards observable objects being better. So accessing primitives through state now returns observables like anything else.

Raw primitives:

  • Pro: Easy to work with
  • Con: Required obs() to get the observable to pass to props or render directly
  • Con: Easy to track a value without realizing it

Observable primitives

  • Pro: More consistent
  • Pro: Easier to deal with undefined
  • Pro: Can dot through undefined paths easily
  • Pro: Doesn’t need obs() or set by key
  • Pro: Easier to use fine-grained features without obs() everywhere
  • Pro: Easier to pass as props without needing obs()
  • Con: Requires get() for primitives

Changes to make:

get()

Wherever you were accessing primitives directly, add a .get() to the end of it.

set(key, value)

Change set by key to access the node first. It will now work fine if the node is undefined.

From: state.profile.set('name', 'Annyong')

To:     state.profile.name.set('Annyong')

obs()

Just remove it. The default behavior is now the same as what obs() did before.

Hooks renamed

useComputed is now useSelector, re-rendering only when the return value changes.

useComputed now returns a computed observable.

0.15 to 0.16

enableLegendStateReact() to observe, removed observer

Legend-State now automatically tracks observable access in any component. To set it up, just call enableLegendStateReact() at the beginning of your app.

Now observer is no longer needed, so just remove all usage of observer.

0.14 to 0.15

Safety

There are now three levels of safety: Unsafe, Default, and Safe. Default is new and allows direct assignment to primitives but prevents directly assigning to everything else. The previous default behavior was Unsafe so you may see errors if you were directly assigning to objects/arrays/etc... TypeScript should show errors so it should be easy to find them. Replace those with .set(...) or pass in false as the second parameter to observable to go back to "Unsafe" mode.

// 1. Unsafe: Use false for the previous unsafe behavior
const obs = observable({ ... }, /*safe*/ false)

// 2. Default: The new default behavior prevent directly assigning to objects, but allows directly assining to primitives
const obs = observable({ text: 'hello',  obj: {} })

obs.text = 'hi'
// ✅ Setting a primitive works in default mode but not in safe mode.

obs.obj = {}
// ❌ Error. Cannot assign to objects directly.

// 3. Safe: Safe mode prevents all direct assignment
const obs = observable({ text: 'hello',  obj: {} }, /*safe*/true)

obs.text = 'hi'
// ❌ Error. Cannot assign directly in safe mode.

Renamed ref to obs

ref was a bit unclear and conflicted with React - the new feature to directly render observables requires a ref property. So it is now renamed to obs, which feels more intuitive as it is used to get an observable.

const state = observable({ text: '' })

// Before
const textRef = state.ref('text')
const textRef2 = state.text.ref()

// Now
const textObs = obs.obs('text')
const textObs2 = obs.text.obs()

Array optimizations

The array optimizations are now opt-in, because they are only useful in React and can potentially have some unexpected behavior in React if modifying the DOM externally. You can enable them by using the For component with the optimized prop. See Arrays for more.

const obs = observable({ items: [] })

const Row = observer(function Row({ item }) {
    return <div>{item.text}</div>
})

const List = observer(function () {
    // The optimized prop enables the optimizations which were previously default
    return <For each={list} item={Row} optimized />
})

Shallow

Since there's now a additionally the optimized tracking for arrays, the shallow option on get() and obs() now has another option. So instead of passing shallow to an observable, use the Tracking namespace now.

import { Tracking } from '@legendapp/state'

const obs = observable([])

// Before
obs.get(shallow)

// Now
obs.get(Tracking.shallow)

Batching

The observableBatcher namespace is removed and the batching functions are now exported on their own.

import { batch, beginBatch, endBatch } from '@legendapp/state'

// begin/end
beginBatch()
obs1.set(...)
obs2.set(...)
endBatch()

// batch()
batch(() => {
    obs1.set(...)
    obs2.set(...)
}

Change functions => observe/when

The new observe and when functions can automatically track all observables accessed while running them. This made the old extra change utilities unnecessary, so onTrue, onHasValue, onEquals, and onChangeShallow have been removed, saving 200 bytes (7%) from the bundle size. These are the new equivalents:

import { observe, when } from "@legendapp/state"

const obs = observable({ value: undefined })

// onTrue
obs.value.onTrue(handler)
// New onTrue equivalent
when(() => obs.value === true, handler)

// onHasValue
obs.value.onHasValue('text', handler)
// onHasValue equivalent
when(() => obs.value, handler)

// onEquals
obs.value.onEquals('text', handler)
// onEquals equivalent
when(() => obs.value === 'text', handler)

// onChangeShallow
obs.value.onChangeShallow(handler)
// onChangeShallow equivalent
obs.value.onChange(handler, { shallow: true })

Primitive current => value

Primitive observables are now wrapped in { value } instead of { current }. You can also now modify the value directly.

const obs = observable(10)
// Before
obs.current === 10
obs.curent = 20 // ❌ Error
// Now
obs.value === 10
obs.value = 20 // ✅ Works

Renamed observableComputed and observableEvent

observableComputed is now just computed and observableEvent is now just event.

import { computed, event } from '@legendapp/state'

// Before
const value = observableComputed(() => ...)
// Now
const value = computed(() => ...)

// Before
const evt = observableEvent(() => ...)
// Now
const evt = event(() => ...)

Renamed LS to Bindable

The automatically bound exports are now named better and in their own exports, so change your exports from LS to:

// Web
import { Bindable } from '@legendapp/state/react-components'

// React Native
import { Bindable } from '@legendapp/state/react-native-components'

Renamed Isolate to Computed

The control flow component Isolate is renamed to Computed for naming consistency.

Removed memo and isolate props

We found these confusing in practice as it wasn't super clear when a component was getting memoized, and it's not much extra work to use the Memo and Computed components directly. If you were using those, switch to the Computed and Memo components instead

// Before
<div memo>...</div>
<div computed>...</div>

// Now
<Memo><div>...</div></Memo>
<Computed><div>...</div></Computed>