Skip to content

Latest commit

 

History

History
51 lines (38 loc) · 1.38 KB

File metadata and controls

51 lines (38 loc) · 1.38 KB
title createMutable

createMutable creates a new mutable Store proxy object that provides a way to selectively trigger updates only when values change.

By intercepting property access, it allows automatic tracking of deep nesting via proxy making it useful for integrating external systems or serving as a compatibility layer with frameworks like MobX or Vue.

function createMutable<T extends StoreNode>(state: T | Store<T>): Store<T>;
It's important to recognize that a mutable state, which can be passed around and modified anywhere, may complicate the code structure and increase the risk of breaking unidirectional flow.
For a more robust alternative, it is generally recommended to use `createStore` instead.
Additionally, the [`produce`](/reference/store-utilities/produce) utility can provide many of these same benefits without the associated downsides.
import { createMutable } from "solid-js/store";

const state = createMutable({
	someValue: 0,
	list: [],
});

// read value
state.someValue;

// set value
state.someValue = 5;

state.list.push(anotherValue);

Mutables support setters along with getters.

const user = createMutable({
	firstName: "John",
	lastName: "Smith",
	get fullName() {
		return `${this.firstName} ${this.lastName}`;
	},
	set setFullName(value) {
		[this.firstName, this.lastName] = value.split(" ");
	},
});