Store utilities

produce

Edit this page

produce creates a store modifier that applies changes by mutating the current state through a helper proxy.


Import

import { produce } from "solid-js/store";

Type

function produce<T>(fn: (state: T) => void): (state: T) => T;

Parameters

fn

  • Type: (state: T) => void

Function that mutates the provided proxy state.


Return value

  • Type: (state: T) => T

Store modifier function.


Behavior

  • produce returns a function that can be passed to store setters or modifyMutable.
  • The returned modifier mutates the passed state and returns that same state.
  • The helper is primarily for store objects and nested wrappable data.

Examples

Basic usage

import { createStore, produce } from "solid-js/store";
const [state, setState] = createStore({
user: {
name: "John",
},
list: ["book"],
});
setState(
produce((state) => {
state.user.name = "Jane";
state.list.push("pen");
})
);

Report an issue with this page