SolidJSS
SolidJSโ€ข3y agoโ€ข
1 reply
TripleSmile

What are the differences? Produce vs regular setStore

This is Astro + SolidJS components.

Question:
Besides produce allowing to neatly update specific properties of an object, are there any other differences? Any performance nuances between these 2 approaches dealing with stores?
import { createStore, produce } from "solid-js/store";
export function SolidComponent() {
    const [solidStore, setSolidStore] = createStore({ a: "aaa", b: "bbb" });
    function updateA() {
        console.log("A");
        setSolidStore((obj) => {
            return { a: obj.a + "A", b: obj.b };
        });
    }

    function updateB() {
        console.log("B");
        setSolidStore(produce((obj) => (obj.a += "B")));
    }

    return (
        <>
            Yoooo
            <p>
                from store - a: {solidStore.a} and b: {solidStore.b}
            </p>
            <button onClick={() => updateA()}>Press me A!</button>
            <button onClick={() => updateB()}>Press me B!</button>
        </>
    );
}

P.S. sorry for bad naming, was just playing around.
image.png
Was this page helpful?