Configuring
Legend-State is designed to have a lean core that allows you and your team to add additional features, so it has configuration functions to add features as you like.
These functions add features and augment the TypeScript interface to add the new functions, so just importing the file adds the interface.
These configuration functions only need to be called once, before their effects are used, and then they will work anywhere. It should generally be at the top of the file that’s the entry point of your app or is imported everywhere, or it could be at the top of a global state file.
enable$GetSet
This enables accessing and setting the raw value of an observable directly. It’s a shorthand for get()
and set(...)
.
Now you can access/modify observables directly.
enable_PeekAssign
This enables accessing and setting the raw value of an observable directly without tracking or notifying listeners. Getting with ._
is a shorthand for peek()
and assigning to ._
modifies the underlying data without notifying. Modifying data without notifying is likely necessary in only very specific scenarios so use it with care.
Now you can access/modify observables directly without notifying.
enableReactTracking
enableReactTracking
has two modes, to either enable automatic tracking or warn if you forgot observer
.
warnUnobserved
This makes React components warn if using get()
without being wrapped in observer
. This is a very helpful way to catch the easy mistake of forgetting observer
.
It only runs when process.env.NODE_ENV === 'development'
so it won’t disrupt your app in production.
auto tracking
This makes React components auto-track observables without using observer
, so all you need to do is get()
an observable and the component will re-render when it changes. This is useful for rapid prototyping as observing is done for you.
Now you can just get()
and components will be automatically reactive.
Note that enableReactTracking
and observer
can be used together - observer will optimize away the auto tracking behavior in favor of its more efficient tracking.