createMutable
编辑此页面createMutable
创建一个新的可变 Store 代理对象,它提供了一种方式,仅在值发生改变时才触发更新。
通过拦截属性访问,它允许通过代理自动跟踪深层嵌套,这使得它在集成外部系统或作为与 MobX 或 Vue 等框架的兼容层时非常有用。
import { createMutable } from "solid-js/store"import type { Store, StoreNode } from "solid-js/store"
function createMutable<T extends StoreNode>(state: T | Store<T>): Store<T>;
Info:
重要的是要认识到,可变状态可以在任何地方传递和修改,这可能会使代码结构变得复杂,并增加破坏单向数据流的风险。
作为一个更稳健的替代方案,通常建议使用 createStore
来代替。
此外,produce
工具可以提供许多相同的好处,而不会带来相关的缺点。
import { createMutable } from "solid-js/store"
const state = createMutable({ someValue: 0, list: [],});
// 读取值state.someValue;
// 设置值state.someValue = 5;
state.list.push(anotherValue);
Mutables 支持 setter 和 getter。
const user = createMutable({ firstName: "John", lastName: "Smith", get fullName() { return `${this.firstName} ${this.lastName}`; }, set setFullName(value) { [this.firstName, this.lastName] = value.split(" "); },});