Store Utilities

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>;
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(" ");
},
});
报告此页面问题